OSDN Git Service

5690583e5d1dc9142c8656c8dc175614494501b3
[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         private static AppendSettingDialog _instance = new AppendSettingDialog();
49         private Twitter tw;
50
51         private bool _ValidationError = false;
52         private MyCommon.EVENTTYPE _MyEventNotifyFlag;
53         private MyCommon.EVENTTYPE _isMyEventNotifyFlag;
54
55         public List<UserAccount> UserAccounts;
56         private long? InitialUserId;
57         public bool IsRemoveSameEvent;
58
59         public TwitterConfiguration TwitterConfiguration = TwitterConfiguration.DefaultConfiguration();
60
61         private string _pin;
62
63         public event EventHandler<IntervalChangedEventArgs> IntervalChanged;
64
65         public void LoadConfig(SettingCommon settingCommon, SettingLocal settingLocal)
66         {
67             this.GetPeriodPanel.LoadConfig(settingCommon);
68             this.StartupPanel.LoadConfig(settingCommon);
69             this.TweetPrvPanel.LoadConfig(settingCommon);
70             this.TweetActPanel.LoadConfig(settingCommon, settingLocal);
71             this.ActionPanel.LoadConfig(settingCommon, settingLocal);
72             this.FontPanel.LoadConfig(settingLocal);
73             this.FontPanel2.LoadConfig(settingLocal);
74             this.PreviewPanel.LoadConfig(settingCommon);
75             this.GetCountPanel.LoadConfig(settingCommon);
76             this.ShortUrlPanel.LoadConfig(settingCommon);
77             this.ProxyPanel.LoadConfig(settingLocal);
78             this.CooperatePanel.LoadConfig(settingCommon);
79             this.ConnectionPanel.LoadConfig(settingCommon);
80         }
81
82         public void SaveConfig(SettingCommon settingCommon, SettingLocal settingLocal)
83         {
84             this.GetPeriodPanel.SaveConfig(settingCommon);
85             this.StartupPanel.SaveConfig(settingCommon);
86             this.TweetPrvPanel.SaveConfig(settingCommon);
87             this.TweetActPanel.SaveConfig(settingCommon, settingLocal);
88             this.ActionPanel.SaveConfig(settingCommon, settingLocal);
89             this.FontPanel.SaveConfig(settingLocal);
90             this.FontPanel2.SaveConfig(settingLocal);
91             this.PreviewPanel.SaveConfig(settingCommon);
92             this.GetCountPanel.SaveConfig(settingCommon);
93             this.ShortUrlPanel.SaveConfig(settingCommon);
94             this.ProxyPanel.SaveConfig(settingLocal);
95             this.CooperatePanel.SaveConfig(settingCommon);
96             this.ConnectionPanel.SaveConfig(settingCommon);
97         }
98
99         private void TreeViewSetting_BeforeSelect(object sender, TreeViewCancelEventArgs e)
100         {
101             if (this.TreeViewSetting.SelectedNode == null) return;
102             var pnl = (SettingPanelBase)this.TreeViewSetting.SelectedNode.Tag;
103             if (pnl == null) return;
104             pnl.Enabled = false;
105             pnl.Visible = false;
106         }
107
108         private void TreeViewSetting_AfterSelect(object sender, TreeViewEventArgs e)
109         {
110             if (e.Node == null) return;
111             var pnl = (SettingPanelBase)e.Node.Tag;
112             if (pnl == null) return;
113             pnl.Enabled = true;
114             pnl.Visible = true;
115
116             if (pnl.Name == "PreviewPanel")
117             {
118                 if (GrowlHelper.IsDllExists)
119                 {
120                     this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = true;
121                 }
122                 else
123                 {
124                     this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = false;
125                 }
126             }
127         }
128
129         private void Save_Click(object sender, EventArgs e)
130         {
131             if (MyCommon.IsNetworkAvailable() &&
132                 (this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Bitly || this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Jmp))
133             {
134                 // bit.ly 短縮機能実装のプライバシー問題の暫定対応
135                 // bit.ly 使用時はログインIDとAPIキーの指定を必須とする
136                 // 参照: http://sourceforge.jp/projects/opentween/lists/archive/dev/2012-January/000020.html
137                 if (string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyId.Text) || string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyPw.Text))
138                 {
139                     MessageBox.Show("bit.ly のログイン名とAPIキーの指定は必須項目です。", Application.ProductName);
140                     _ValidationError = true;
141                     TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択
142                     TreeViewSetting.Select();
143                     this.ShortUrlPanel.TextBitlyId.Focus();
144                     return;
145                 }
146
147                 if (!BitlyValidation(this.ShortUrlPanel.TextBitlyId.Text, this.ShortUrlPanel.TextBitlyPw.Text))
148                 {
149                     MessageBox.Show(Properties.Resources.SettingSave_ClickText1);
150                     _ValidationError = true;
151                     TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択
152                     TreeViewSetting.Select();
153                     this.ShortUrlPanel.TextBitlyId.Focus();
154                     return;
155                 }
156                 else
157                 {
158                     _ValidationError = false;
159                 }
160             }
161             else
162             {
163                 _ValidationError = false;
164             }
165
166             this.UserAccounts.Clear();
167             foreach (object u in this.BasedPanel.AuthUserCombo.Items)
168             {
169                 this.UserAccounts.Add((UserAccount)u);
170             }
171             if (this.BasedPanel.AuthUserCombo.SelectedIndex > -1)
172             {
173                 foreach (UserAccount u in this.UserAccounts)
174                 {
175                     if (u.Username.ToLower() == ((UserAccount)this.BasedPanel.AuthUserCombo.SelectedItem).Username.ToLower())
176                     {
177                         tw.Initialize(u.Token, u.TokenSecret, u.Username, u.UserId);
178                         if (u.UserId == 0)
179                         {
180                             tw.VerifyCredentials();
181                             u.UserId = tw.UserId;
182                         }
183                         break;
184                     }
185                 }
186             }
187             else
188             {
189                 tw.ClearAuthInfo();
190                 tw.Initialize("", "", "", 0);
191             }
192
193 #if UA
194             //フォロー
195             if (this.FollowCheckBox.Checked)
196             {
197                 //現在の設定内容で通信
198                 HttpConnection.ProxyType ptype;
199                 if (RadioProxyNone.Checked)
200                 {
201                     ptype = HttpConnection.ProxyType.None;
202                 }
203                 else if (RadioProxyIE.Checked)
204                 {
205                     ptype = HttpConnection.ProxyType.IE;
206                 }
207                 else
208                 {
209                     ptype = HttpConnection.ProxyType.Specified;
210                 }
211                 string padr = TextProxyAddress.Text.Trim();
212                 int pport = int.Parse(TextProxyPort.Text.Trim());
213                 string pusr = TextProxyUser.Text.Trim();
214                 string ppw = TextProxyPassword.Text.Trim();
215                 HttpConnection.InitializeConnection(20, ptype, padr, pport, pusr, ppw);
216
217                 string ret = tw.PostFollowCommand(ApplicationSettings.FeedbackTwitterName);
218             }
219 #endif
220             try
221             {
222                 EventNotifyEnabled = this.NotifyPanel.CheckEventNotify.Checked;
223                 GetEventNotifyFlag(ref _MyEventNotifyFlag, ref _isMyEventNotifyFlag);
224                 ForceEventNotify = this.NotifyPanel.CheckForceEventNotify.Checked;
225                 FavEventUnread = this.NotifyPanel.CheckFavEventUnread.Checked;
226                 EventSoundFile = (string)this.NotifyPanel.ComboBoxEventNotifySound.SelectedItem;
227                 this.IsRemoveSameEvent = this.NotifyPanel.IsRemoveSameFavEventCheckBox.Checked;
228             }
229             catch(Exception)
230             {
231                 MessageBox.Show(Properties.Resources.Save_ClickText3);
232                 this.DialogResult = DialogResult.Cancel;
233                 return;
234             }
235         }
236
237         private void Setting_FormClosing(object sender, FormClosingEventArgs e)
238         {
239             if (MyCommon._endingFlag) return;
240
241             if (this.DialogResult == DialogResult.Cancel)
242             {
243                 //キャンセル時は画面表示時のアカウントに戻す
244                 //キャンセル時でも認証済みアカウント情報は保存する
245                 this.UserAccounts.Clear();
246                 foreach (object u in this.BasedPanel.AuthUserCombo.Items)
247                 {
248                     this.UserAccounts.Add((UserAccount)u);
249                 }
250                 //アクティブユーザーを起動時のアカウントに戻す(起動時アカウントなければ何もしない)
251                 bool userSet = false;
252                 if (this.InitialUserId != null)
253                 {
254                     foreach (UserAccount u in this.UserAccounts)
255                     {
256                         if (u.UserId == this.InitialUserId)
257                         {
258                             tw.Initialize(u.Token, u.TokenSecret, u.Username, u.UserId);
259                             userSet = true;
260                             break;
261                         }
262                     }
263                 }
264                 //認証済みアカウントが削除されていた場合、もしくは起動時アカウントがなかった場合は、
265                 //アクティブユーザーなしとして初期化
266                 if (!userSet)
267                 {
268                     tw.ClearAuthInfo();
269                     tw.Initialize("", "", "", 0);
270                 }
271             }
272
273             if (tw != null && string.IsNullOrEmpty(tw.Username) && e.CloseReason == CloseReason.None)
274             {
275                 if (MessageBox.Show(Properties.Resources.Setting_FormClosing1, "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
276                 {
277                     e.Cancel = true;
278                 }
279             }
280             if (_ValidationError)
281             {
282                 e.Cancel = true;
283             }
284             if (e.Cancel == false && TreeViewSetting.SelectedNode != null)
285             {
286                 var curPanel = (SettingPanelBase)TreeViewSetting.SelectedNode.Tag;
287                 curPanel.Visible = false;
288                 curPanel.Enabled = false;
289             }
290         }
291
292         private void Setting_Load(object sender, EventArgs e)
293         {
294             tw = ((TweenMain)this.Owner).TwitterInstance;
295             //this.AuthStateLabel.Enabled = true;
296             //this.AuthUserLabel.Enabled = true;
297             this.BasedPanel.AuthClearButton.Enabled = true;
298
299             //if (tw.Username == "")
300             //{
301             //    //this.AuthStateLabel.Text = Properties.Resources.AuthorizeButton_Click4
302             //    //this.AuthUserLabel.Text = ""
303             //    //this.Save.Enabled = false
304             //}
305             //else
306             //{
307             //    //this.AuthStateLabel.Text = Properties.Resources.AuthorizeButton_Click3;
308             //    //if (TwitterApiInfo.AccessLevel == ApiAccessLevel.ReadWrite)
309             //    //{
310             //    //    this.AuthStateLabel.Text += "(xAuth)";
311             //    //}
312             //    //else if (TwitterApiInfo.AccessLevel == ApiAccessLevel.ReadWriteAndDirectMessage)
313             //    //{
314             //    //    this.AuthStateLabel.Text += "(OAuth)";
315             //    //}
316             //    //this.AuthUserLabel.Text = tw.Username;
317             //}
318
319             this.BasedPanel.AuthUserCombo.Items.Clear();
320             if (this.UserAccounts.Count > 0)
321             {
322                 this.BasedPanel.AuthUserCombo.Items.AddRange(this.UserAccounts.ToArray());
323                 foreach (UserAccount u in this.UserAccounts)
324                 {
325                     if (u.UserId == tw.UserId)
326                     {
327                         this.BasedPanel.AuthUserCombo.SelectedItem = u;
328                         this.InitialUserId = u.UserId;
329                         break;
330                     }
331                 }
332             }
333
334             ApplyEventNotifyFlag(EventNotifyEnabled, EventNotifyFlag, IsMyEventNotifyFlag);
335             this.NotifyPanel.CheckForceEventNotify.Checked = ForceEventNotify;
336             this.NotifyPanel.CheckFavEventUnread.Checked = FavEventUnread;
337             SoundFileListup();
338
339             this.NotifyPanel.IsRemoveSameFavEventCheckBox.Checked = this.IsRemoveSameEvent;
340
341             this.TreeViewSetting.Nodes["BasedNode"].Tag = BasedPanel;
342             this.TreeViewSetting.Nodes["BasedNode"].Nodes["PeriodNode"].Tag = GetPeriodPanel;
343             this.TreeViewSetting.Nodes["BasedNode"].Nodes["StartUpNode"].Tag = StartupPanel;
344             this.TreeViewSetting.Nodes["BasedNode"].Nodes["GetCountNode"].Tag = GetCountPanel;
345             //this.TreeViewSetting.Nodes["BasedNode"].Nodes["UserStreamNode"].Tag = UserStreamPanel;
346             this.TreeViewSetting.Nodes["ActionNode"].Tag = ActionPanel;
347             this.TreeViewSetting.Nodes["ActionNode"].Nodes["TweetActNode"].Tag = TweetActPanel;
348             this.TreeViewSetting.Nodes["PreviewNode"].Tag = PreviewPanel;
349             this.TreeViewSetting.Nodes["PreviewNode"].Nodes["TweetPrvNode"].Tag = TweetPrvPanel;
350             this.TreeViewSetting.Nodes["PreviewNode"].Nodes["NotifyNode"].Tag = NotifyPanel;
351             this.TreeViewSetting.Nodes["FontNode"].Tag = FontPanel;
352             this.TreeViewSetting.Nodes["FontNode"].Nodes["FontNode2"].Tag = FontPanel2;
353             this.TreeViewSetting.Nodes["ConnectionNode"].Tag = ConnectionPanel;
354             this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ProxyNode"].Tag = ProxyPanel;
355             this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["CooperateNode"].Tag = CooperatePanel;
356             this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"].Tag = ShortUrlPanel;
357
358             this.TreeViewSetting.SelectedNode = this.TreeViewSetting.Nodes[0];
359             this.TreeViewSetting.ExpandAll();
360
361             //TreeViewSetting.SelectedNode = TreeViewSetting.TopNode;
362             ActiveControl = BasedPanel.StartAuthButton;
363         }
364
365         private void UReadMng_CheckedChanged(object sender, EventArgs e)
366         {
367             if (this.ActionPanel.UReadMng.Checked == true)
368             {
369                 this.StartupPanel.StartupReaded.Enabled = true;
370             }
371             else
372             {
373                 this.StartupPanel.StartupReaded.Enabled = false;
374             }
375         }
376
377         private void btnFontAndColor_Click(object sender, EventArgs e) // Handles btnUnread.Click, btnDetail.Click, btnListFont.Click, btnInputFont.Click
378         {
379             Button Btn = (Button) sender;
380             DialogResult rtn;
381
382             FontDialog1.AllowVerticalFonts = false;
383             FontDialog1.AllowScriptChange = true;
384             FontDialog1.AllowSimulations = true;
385             FontDialog1.AllowVectorFonts = true;
386             FontDialog1.FixedPitchOnly = false;
387             FontDialog1.FontMustExist = true;
388             FontDialog1.ScriptsOnly = false;
389             FontDialog1.ShowApply = false;
390             FontDialog1.ShowEffects = true;
391             FontDialog1.ShowColor = true;
392
393             switch (Btn.Name)
394             {
395                 case "btnUnread":
396                     FontDialog1.Color = this.FontPanel.lblUnread.ForeColor;
397                     FontDialog1.Font = this.FontPanel.lblUnread.Font;
398                     break;
399                 case "btnDetail":
400                     FontDialog1.Color = this.FontPanel.lblDetail.ForeColor;
401                     FontDialog1.Font = this.FontPanel.lblDetail.Font;
402                     break;
403                 case "btnListFont":
404                     FontDialog1.Color = this.FontPanel.lblListFont.ForeColor;
405                     FontDialog1.Font = this.FontPanel.lblListFont.Font;
406                     break;
407                 case "btnInputFont":
408                     FontDialog1.Color = this.FontPanel2.lblInputFont.ForeColor;
409                     FontDialog1.Font = this.FontPanel2.lblInputFont.Font;
410                     break;
411             }
412
413             try
414             {
415                 rtn = FontDialog1.ShowDialog();
416             }
417             catch(ArgumentException ex)
418             {
419                 MessageBox.Show(ex.Message);
420                 return;
421             }
422
423             if (rtn == DialogResult.Cancel) return;
424
425             switch (Btn.Name)
426             {
427                 case "btnUnread":
428                     this.FontPanel.lblUnread.ForeColor = FontDialog1.Color;
429                     this.FontPanel.lblUnread.Font = FontDialog1.Font;
430                     break;
431                 case "btnDetail":
432                     this.FontPanel.lblDetail.ForeColor = FontDialog1.Color;
433                     this.FontPanel.lblDetail.Font = FontDialog1.Font;
434                     break;
435                 case "btnListFont":
436                     this.FontPanel.lblListFont.ForeColor = FontDialog1.Color;
437                     this.FontPanel.lblListFont.Font = FontDialog1.Font;
438                     break;
439                 case "btnInputFont":
440                     this.FontPanel2.lblInputFont.ForeColor = FontDialog1.Color;
441                     this.FontPanel2.lblInputFont.Font = FontDialog1.Font;
442                     break;
443             }
444
445         }
446
447         private void btnColor_Click(object sender, EventArgs e) //Handles btnSelf.Click, btnAtSelf.Click, btnTarget.Click, btnAtTarget.Click, btnAtFromTarget.Click, btnFav.Click, btnOWL.Click, btnInputBackcolor.Click, btnAtTo.Click, btnListBack.Click, btnDetailBack.Click, btnDetailLink.Click, btnRetweet.Click
448         {
449             Button Btn = (Button)sender;
450             DialogResult rtn;
451
452             ColorDialog1.AllowFullOpen = true;
453             ColorDialog1.AnyColor = true;
454             ColorDialog1.FullOpen = false;
455             ColorDialog1.SolidColorOnly = false;
456
457             switch (Btn.Name)
458             {
459                 case "btnSelf":
460                     ColorDialog1.Color = this.FontPanel2.lblSelf.BackColor;
461                     break;
462                 case "btnAtSelf":
463                     ColorDialog1.Color = this.FontPanel2.lblAtSelf.BackColor;
464                     break;
465                 case "btnTarget":
466                     ColorDialog1.Color = this.FontPanel2.lblTarget.BackColor;
467                     break;
468                 case "btnAtTarget":
469                     ColorDialog1.Color = this.FontPanel2.lblAtTarget.BackColor;
470                     break;
471                 case "btnAtFromTarget":
472                     ColorDialog1.Color = this.FontPanel2.lblAtFromTarget.BackColor;
473                     break;
474                 case "btnFav":
475                     ColorDialog1.Color = this.FontPanel.lblFav.ForeColor;
476                     break;
477                 case "btnOWL":
478                     ColorDialog1.Color = this.FontPanel.lblOWL.ForeColor;
479                     break;
480                 case "btnRetweet":
481                     ColorDialog1.Color = this.FontPanel.lblRetweet.ForeColor;
482                     break;
483                 case "btnInputBackcolor":
484                     ColorDialog1.Color = this.FontPanel2.lblInputBackcolor.BackColor;
485                     break;
486                 case "btnAtTo":
487                     ColorDialog1.Color = this.FontPanel2.lblAtTo.BackColor;
488                     break;
489                 case "btnListBack":
490                     ColorDialog1.Color = this.FontPanel2.lblListBackcolor.BackColor;
491                     break;
492                 case "btnDetailBack":
493                     ColorDialog1.Color = this.FontPanel.lblDetailBackcolor.BackColor;
494                     break;
495                 case "btnDetailLink":
496                     ColorDialog1.Color = this.FontPanel.lblDetailLink.ForeColor;
497                     break;
498             }
499
500             rtn = ColorDialog1.ShowDialog();
501
502             if (rtn == DialogResult.Cancel) return;
503
504             switch (Btn.Name)
505             {
506                 case "btnSelf":
507                     this.FontPanel2.lblSelf.BackColor = ColorDialog1.Color;
508                     break;
509                 case "btnAtSelf":
510                     this.FontPanel2.lblAtSelf.BackColor = ColorDialog1.Color;
511                     break;
512                 case "btnTarget":
513                     this.FontPanel2.lblTarget.BackColor = ColorDialog1.Color;
514                     break;
515                 case "btnAtTarget":
516                     this.FontPanel2.lblAtTarget.BackColor = ColorDialog1.Color;
517                     break;
518                 case "btnAtFromTarget":
519                     this.FontPanel2.lblAtFromTarget.BackColor = ColorDialog1.Color;
520                     break;
521                 case "btnFav":
522                     this.FontPanel.lblFav.ForeColor = ColorDialog1.Color;
523                     break;
524                 case "btnOWL":
525                     this.FontPanel.lblOWL.ForeColor = ColorDialog1.Color;
526                     break;
527                 case "btnRetweet":
528                     this.FontPanel.lblRetweet.ForeColor = ColorDialog1.Color;
529                     break;
530                 case "btnInputBackcolor":
531                     this.FontPanel2.lblInputBackcolor.BackColor = ColorDialog1.Color;
532                     break;
533                 case "btnAtTo":
534                     this.FontPanel2.lblAtTo.BackColor = ColorDialog1.Color;
535                     break;
536                 case "btnListBack":
537                     this.FontPanel2.lblListBackcolor.BackColor = ColorDialog1.Color;
538                     break;
539                 case "btnDetailBack":
540                     this.FontPanel.lblDetailBackcolor.BackColor = ColorDialog1.Color;
541                     break;
542                 case "btnDetailLink":
543                     this.FontPanel.lblDetailLink.ForeColor = ColorDialog1.Color;
544                     break;
545             }
546         }
547
548         public string RecommendStatusText { get; set; }
549
550         public bool EventNotifyEnabled { get; set; }
551
552         public MyCommon.EVENTTYPE EventNotifyFlag
553         {
554             get
555             {
556                 return _MyEventNotifyFlag;
557             }
558             set
559             {
560                 _MyEventNotifyFlag = value;
561             }
562         }
563
564         public MyCommon.EVENTTYPE IsMyEventNotifyFlag
565         {
566             get
567             {
568                 return _isMyEventNotifyFlag;
569             }
570             set
571             {
572                 _isMyEventNotifyFlag = value;
573             }
574         }
575
576         public bool ForceEventNotify { get; set; }
577         public bool FavEventUnread { get; set; }
578
579         public string EventSoundFile { get; set; }
580
581         private bool StartAuth()
582         {
583             //現在の設定内容で通信
584             ProxyType ptype;
585             if (this.ProxyPanel.RadioProxyNone.Checked)
586             {
587                 ptype = ProxyType.None;
588             }
589             else if (this.ProxyPanel.RadioProxyIE.Checked)
590             {
591                 ptype = ProxyType.IE;
592             }
593             else
594             {
595                 ptype = ProxyType.Specified;
596             }
597             string padr = this.ProxyPanel.TextProxyAddress.Text.Trim();
598             int pport = int.Parse(this.ProxyPanel.TextProxyPort.Text.Trim());
599             string pusr = this.ProxyPanel.TextProxyUser.Text.Trim();
600             string ppw = this.ProxyPanel.TextProxyPassword.Text.Trim();
601
602             //通信基底クラス初期化
603             Networking.DefaultTimeout = TimeSpan.FromSeconds(20);
604             Networking.SetWebProxy(ptype, padr, pport, pusr, ppw);
605             HttpTwitter.TwitterUrl = this.ConnectionPanel.TwitterAPIText.Text.Trim();
606             tw.Initialize("", "", "", 0);
607             //this.AuthStateLabel.Text = Properties.Resources.AuthorizeButton_Click4;
608             //this.AuthUserLabel.Text = "";
609             string pinPageUrl = "";
610             string rslt = tw.StartAuthentication(ref pinPageUrl);
611             if (string.IsNullOrEmpty(rslt))
612             {
613                 string pin = AuthDialog.DoAuth(this, pinPageUrl);
614                 if (!string.IsNullOrEmpty(pin))
615                 {
616                     this._pin = pin;
617                     return true;
618                 }
619                 else
620                 {
621                     return false;
622                 }
623             }
624             else
625             {
626                 MessageBox.Show(Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + rslt, "Authenticate", MessageBoxButtons.OK);
627                 return false;
628             }
629         }
630
631         private bool PinAuth()
632         {
633             string pin = this._pin;   //PIN Code
634
635             string rslt = tw.Authenticate(pin);
636             if (string.IsNullOrEmpty(rslt))
637             {
638                 MessageBox.Show(Properties.Resources.AuthorizeButton_Click1, "Authenticate", MessageBoxButtons.OK);
639                 //this.AuthStateLabel.Text = Properties.Resources.AuthorizeButton_Click3;
640                 //this.AuthUserLabel.Text = tw.Username;
641                 int idx = -1;
642                 UserAccount user = new UserAccount();
643                 user.Username = tw.Username;
644                 user.UserId = tw.UserId;
645                 user.Token = tw.AccessToken;
646                 user.TokenSecret = tw.AccessTokenSecret;
647
648                 foreach (object u in this.BasedPanel.AuthUserCombo.Items)
649                 {
650                     if (((UserAccount)u).Username.ToLower() == tw.Username.ToLower())
651                     {
652                         idx = this.BasedPanel.AuthUserCombo.Items.IndexOf(u);
653                         break;
654                     }
655                 }
656                 if (idx > -1)
657                 {
658                     this.BasedPanel.AuthUserCombo.Items.RemoveAt(idx);
659                     this.BasedPanel.AuthUserCombo.Items.Insert(idx, user);
660                     this.BasedPanel.AuthUserCombo.SelectedIndex = idx;
661                 }
662                 else
663                 {
664                     this.BasedPanel.AuthUserCombo.SelectedIndex = this.BasedPanel.AuthUserCombo.Items.Add(user);
665                 }
666                 //if (TwitterApiInfo.AccessLevel = ApiAccessLevel.ReadWrite)
667                 //{
668                 //    this.AuthStateLabel.Text += "(xAuth)";
669                 //}
670                 //else if (TwitterApiInfo.AccessLevel == ApiAccessLevel.ReadWriteAndDirectMessage)
671                 //{
672                 //    this.AuthStateLabel.Text += "(OAuth)";
673                 //}
674                 return true;
675             }
676             else
677             {
678                 MessageBox.Show(Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + rslt, "Authenticate", MessageBoxButtons.OK);
679                 //this.AuthStateLabel.Text = Properties.Resources.AuthorizeButton_Click4;
680                 //this.AuthUserLabel.Text = "";
681                 return false;
682             }
683         }
684
685         private void StartAuthButton_Click(object sender, EventArgs e)
686         {
687             //this.Save.Enabled = false;
688             if (StartAuth())
689             {
690                 if (PinAuth())
691                 {
692                     //this.Save.Enabled = true;
693                 }
694             }
695         }
696
697         private void CheckPostAndGet_CheckedChanged(object sender, EventArgs e)
698         {
699             this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamEnabled;
700         }
701
702         private void Setting_Shown(object sender, EventArgs e)
703         {
704             do
705             {
706                 Thread.Sleep(10);
707                 if (this.Disposing || this.IsDisposed) return;
708             } while (!this.IsHandleCreated);
709             this.TopMost = this.PreviewPanel.CheckAlwaysTop.Checked;
710
711             this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamEnabled;
712             this.GetPeriodPanel.LabelUserStreamActive.Visible = tw.UserStreamEnabled;
713         }
714
715         public static AppendSettingDialog Instance
716         {
717             get { return _instance; }
718         }
719
720         private bool BitlyValidation(string id, string apikey)
721         {
722             if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(apikey))
723             {
724                 return false;
725             }
726
727             string req = "http://api.bit.ly/v3/validate";
728             string content = "";
729             Dictionary<string, string> param = new Dictionary<string, string>();
730
731             param.Add("login", ApplicationSettings.BitlyLoginId);
732             param.Add("apiKey", ApplicationSettings.BitlyApiKey);
733             param.Add("x_login", id);
734             param.Add("x_apiKey", apikey);
735             param.Add("format", "txt");
736
737             if (!(new HttpVarious()).PostData(req, param, out content))
738             {
739                 return true;             // 通信エラーの場合はとりあえずチェックを通ったことにする
740             }
741             else if (content.Trim() == "1")
742             {
743                 return true;             // 検証成功
744             }
745             else if (content.Trim() == "0")
746             {
747                 return false;            // 検証失敗 APIキーとIDの組み合わせが違う
748             }
749             else
750             {
751                 return true;             // 規定外応答:通信エラーの可能性があるためとりあえずチェックを通ったことにする
752             }
753         }
754
755         private void Cancel_Click(object sender, EventArgs e)
756         {
757             _ValidationError = false;
758         }
759
760         //private void CheckEventNotify_CheckedChanged(object sender, EventArgs e)
761         //                Handles CheckEventNotify.CheckedChanged, CheckFavoritesEvent.CheckStateChanged,
762         //                        CheckUnfavoritesEvent.CheckStateChanged, CheckFollowEvent.CheckStateChanged,
763         //                        CheckListMemberAddedEvent.CheckStateChanged, CheckListMemberRemovedEvent.CheckStateChanged,
764         //                        CheckListCreatedEvent.CheckStateChanged, CheckUserUpdateEvent.CheckStateChanged
765         //{
766         //    EventNotifyEnabled = CheckEventNotify.Checked;
767         //    GetEventNotifyFlag(EventNotifyFlag, IsMyEventNotifyFlag);
768         //    ApplyEventNotifyFlag(EventNotifyEnabled, EventNotifyFlag, IsMyEventNotifyFlag);
769         //}
770
771         private class EventCheckboxTblElement
772         {
773             public CheckBox CheckBox;
774             public MyCommon.EVENTTYPE Type;
775         }
776
777         private EventCheckboxTblElement[] GetEventCheckboxTable()
778         {
779             EventCheckboxTblElement[] _eventCheckboxTable = new EventCheckboxTblElement[8];
780
781             _eventCheckboxTable[0] = new EventCheckboxTblElement();
782             _eventCheckboxTable[0].CheckBox = this.NotifyPanel.CheckFavoritesEvent;
783             _eventCheckboxTable[0].Type = MyCommon.EVENTTYPE.Favorite;
784
785             _eventCheckboxTable[1] = new EventCheckboxTblElement();
786             _eventCheckboxTable[1].CheckBox = this.NotifyPanel.CheckUnfavoritesEvent;
787             _eventCheckboxTable[1].Type = MyCommon.EVENTTYPE.Unfavorite;
788
789             _eventCheckboxTable[2] = new EventCheckboxTblElement();
790             _eventCheckboxTable[2].CheckBox = this.NotifyPanel.CheckFollowEvent;
791             _eventCheckboxTable[2].Type = MyCommon.EVENTTYPE.Follow;
792
793             _eventCheckboxTable[3] = new EventCheckboxTblElement();
794             _eventCheckboxTable[3].CheckBox = this.NotifyPanel.CheckListMemberAddedEvent;
795             _eventCheckboxTable[3].Type = MyCommon.EVENTTYPE.ListMemberAdded;
796
797             _eventCheckboxTable[4] = new EventCheckboxTblElement();
798             _eventCheckboxTable[4].CheckBox = this.NotifyPanel.CheckListMemberRemovedEvent;
799             _eventCheckboxTable[4].Type = MyCommon.EVENTTYPE.ListMemberRemoved;
800
801             _eventCheckboxTable[5] = new EventCheckboxTblElement();
802             _eventCheckboxTable[5].CheckBox = this.NotifyPanel.CheckBlockEvent;
803             _eventCheckboxTable[5].Type = MyCommon.EVENTTYPE.Block;
804
805             _eventCheckboxTable[6] = new EventCheckboxTblElement();
806             _eventCheckboxTable[6].CheckBox = this.NotifyPanel.CheckUserUpdateEvent;
807             _eventCheckboxTable[6].Type = MyCommon.EVENTTYPE.UserUpdate;
808
809             _eventCheckboxTable[7] = new EventCheckboxTblElement();
810             _eventCheckboxTable[7].CheckBox = this.NotifyPanel.CheckListCreatedEvent;
811             _eventCheckboxTable[7].Type = MyCommon.EVENTTYPE.ListCreated;
812
813             return _eventCheckboxTable;
814         }
815
816         private void GetEventNotifyFlag(ref MyCommon.EVENTTYPE eventnotifyflag, ref MyCommon.EVENTTYPE isMyeventnotifyflag)
817         {
818             MyCommon.EVENTTYPE evt = MyCommon.EVENTTYPE.None;
819             MyCommon.EVENTTYPE myevt = MyCommon.EVENTTYPE.None;
820
821             foreach (EventCheckboxTblElement tbl in GetEventCheckboxTable())
822             {
823                 switch (tbl.CheckBox.CheckState)
824                 {
825                     case CheckState.Checked:
826                         evt = evt | tbl.Type;
827                         myevt = myevt | tbl.Type;
828                         break;
829                     case CheckState.Indeterminate:
830                         evt = evt | tbl.Type;
831                         break;
832                     case CheckState.Unchecked:
833                         break;
834                 }
835             }
836             eventnotifyflag = evt;
837             isMyeventnotifyflag = myevt;
838         }
839
840         private void ApplyEventNotifyFlag(bool rootEnabled, MyCommon.EVENTTYPE eventnotifyflag, MyCommon.EVENTTYPE isMyeventnotifyflag)
841         {
842             MyCommon.EVENTTYPE evt = eventnotifyflag;
843             MyCommon.EVENTTYPE myevt = isMyeventnotifyflag;
844
845             this.NotifyPanel.CheckEventNotify.Checked = rootEnabled;
846
847             foreach (EventCheckboxTblElement tbl in GetEventCheckboxTable())
848             {
849                 if ((evt & tbl.Type) != 0)
850                 {
851                     if ((myevt & tbl.Type) != 0)
852                     {
853                         tbl.CheckBox.CheckState = CheckState.Checked;
854                     }
855                     else
856                     {
857                         tbl.CheckBox.CheckState = CheckState.Indeterminate;
858                     }
859                 }
860                 else
861                 {
862                     tbl.CheckBox.CheckState = CheckState.Unchecked;
863                 }
864                 tbl.CheckBox.Enabled = rootEnabled;
865             }
866
867         }
868
869         private void CheckEventNotify_CheckedChanged(object sender, EventArgs e)
870         {
871             foreach (EventCheckboxTblElement tbl in GetEventCheckboxTable())
872             {
873                 tbl.CheckBox.Enabled = this.NotifyPanel.CheckEventNotify.Checked;
874             }
875         }
876
877         //private void CheckForceEventNotify_CheckedChanged(object sender, EventArgs e)
878         //{
879         //    _MyForceEventNotify = CheckEventNotify.Checked;
880         //}
881
882         //private void CheckFavEventUnread_CheckedChanged(object sender, EventArgs e)
883         //{
884         //    _MyFavEventUnread = CheckFavEventUnread.Checked;
885         //}
886
887         //private void ComboBoxTranslateLanguage_SelectedIndexChanged(object sender, EventArgs e)
888         //{
889         //    _MyTranslateLanguage = (new Google()).GetLanguageEnumFromIndex(ComboBoxTranslateLanguage.SelectedIndex);
890         //}
891
892         private void SoundFileListup()
893         {
894             if (EventSoundFile == null) EventSoundFile = "";
895             this.NotifyPanel.ComboBoxEventNotifySound.Items.Clear();
896             this.NotifyPanel.ComboBoxEventNotifySound.Items.Add("");
897             DirectoryInfo oDir = new DirectoryInfo(Application.StartupPath + Path.DirectorySeparatorChar);
898             if (Directory.Exists(Path.Combine(Application.StartupPath, "Sounds")))
899             {
900                 oDir = oDir.GetDirectories("Sounds")[0];
901             }
902             foreach (FileInfo oFile in oDir.GetFiles("*.wav"))
903             {
904                 this.NotifyPanel.ComboBoxEventNotifySound.Items.Add(oFile.Name);
905             }
906             int idx = this.NotifyPanel.ComboBoxEventNotifySound.Items.IndexOf(EventSoundFile);
907             if (idx == -1) idx = 0;
908             this.NotifyPanel.ComboBoxEventNotifySound.SelectedIndex = idx;
909         }
910
911         //private void ComboBoxEventNotifySound_VisibleChanged(object sender, EventArgs e)
912         //{
913         //    SoundFileListup();
914         //}
915
916         //private void ComboBoxEventNotifySound_SelectedIndexChanged(object sender, EventArgs e)
917         //{
918         //   if (_soundfileListup) return;
919
920         //    _MyEventSoundFile = (string)ComboBoxEventNotifySound.SelectedItem;
921         //}
922
923         private void OpenUrl(string url)
924         {
925             string myPath = url;
926             string path = this.ActionPanel.BrowserPathText.Text;
927             try
928             {
929                 if (!string.IsNullOrEmpty(path))
930                 {
931                     if (path.StartsWith("\"") && path.Length > 2 && path.IndexOf("\"", 2) > -1)
932                     {
933                         int sep = path.IndexOf("\"", 2);
934                         string browserPath = path.Substring(1, sep - 1);
935                         string arg = "";
936                         if (sep < path.Length - 1)
937                         {
938                             arg = path.Substring(sep + 1);
939                         }
940                         myPath = arg + " " + myPath;
941                         System.Diagnostics.Process.Start(browserPath, myPath);
942                     }
943                     else
944                     {
945                         System.Diagnostics.Process.Start(path, myPath);
946                     }
947                 }
948                 else
949                 {
950                     System.Diagnostics.Process.Start(myPath);
951                 }
952             }
953             catch(Exception)
954             {
955 //              MessageBox.Show("ブラウザの起動に失敗、またはタイムアウトしました。" + ex.ToString());
956             }
957         }
958
959         private void CreateAccountButton_Click(object sender, EventArgs e)
960         {
961             this.OpenUrl("https://twitter.com/signup");
962         }
963
964         public AppendSettingDialog()
965         {
966             InitializeComponent();
967
968             this.BasedPanel.StartAuthButton.Click += this.StartAuthButton_Click;
969             this.BasedPanel.CreateAccountButton.Click += this.CreateAccountButton_Click;
970             this.NotifyPanel.CheckEventNotify.CheckedChanged += this.CheckEventNotify_CheckedChanged;
971             this.GetPeriodPanel.CheckPostAndGet.CheckedChanged += this.CheckPostAndGet_CheckedChanged;
972             this.ActionPanel.UReadMng.CheckedChanged += this.UReadMng_CheckedChanged;
973             this.FontPanel.btnUnread.Click += this.btnFontAndColor_Click;
974             this.FontPanel.btnDetail.Click += this.btnFontAndColor_Click;
975             this.FontPanel.btnListFont.Click += this.btnFontAndColor_Click;
976             this.FontPanel.btnFav.Click += this.btnColor_Click;
977             this.FontPanel.btnOWL.Click += this.btnColor_Click;
978             this.FontPanel.btnRetweet.Click += this.btnColor_Click;
979             this.FontPanel.btnDetailBack.Click += this.btnColor_Click;
980             this.FontPanel.btnDetailLink.Click += this.btnColor_Click;
981             this.FontPanel2.btnInputFont.Click += this.btnFontAndColor_Click;
982             this.FontPanel2.btnSelf.Click += this.btnColor_Click;
983             this.FontPanel2.btnAtSelf.Click += this.btnColor_Click;
984             this.FontPanel2.btnTarget.Click += this.btnColor_Click;
985             this.FontPanel2.btnAtTarget.Click += this.btnColor_Click;
986             this.FontPanel2.btnAtFromTarget.Click += this.btnColor_Click;
987             this.FontPanel2.btnInputBackcolor.Click += this.btnColor_Click;
988             this.FontPanel2.btnAtTo.Click += this.btnColor_Click;
989             this.FontPanel2.btnListBack.Click += this.btnColor_Click;
990
991             this.Icon = Properties.Resources.MIcon;
992         }
993
994         private void GetPeriodPanel_IntervalChanged(object sender, IntervalChangedEventArgs e)
995         {
996             if (this.IntervalChanged != null)
997                 this.IntervalChanged(sender, e);
998         }
999     }
1000
1001     public class IntervalChangedEventArgs : EventArgs
1002     {
1003         public bool UserStream;
1004         public bool Timeline;
1005         public bool Reply;
1006         public bool DirectMessage;
1007         public bool PublicSearch;
1008         public bool Lists;
1009         public bool UserTimeline;
1010     }
1011 }