OSDN Git Service

Merge branch 'cleanup-code-analyzer-errors'
[opentween/open-tween.git] / OpenTween / FilterDialog.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-2012 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.Linq.Expressions;
36 using System.Text.RegularExpressions;
37 using System.IO;
38 using System.Collections.Specialized;
39
40 namespace OpenTween
41 {
42     public partial class FilterDialog : OTBaseForm
43     {
44         private EDITMODE _mode;
45         private bool _directAdd;
46         private TabInformations _sts;
47         private string _cur;
48         private List<string> idlist = new List<string>();
49
50         private enum EDITMODE
51         {
52             AddNew,
53             Edit,
54             None,
55         }
56
57         public FilterDialog()
58         {
59             InitializeComponent();
60         }
61
62         private void SetFilters(string tabName)
63         {
64             if (ListTabs.Items.Count == 0) return;
65
66             ListFilters.Items.Clear();
67             ListFilters.Items.AddRange(_sts.Tabs[tabName].GetFilters());
68             if (ListFilters.Items.Count > 0) ListFilters.SelectedIndex = 0;
69
70             if (TabInformations.GetInstance().IsDefaultTab(tabName))
71             {
72                 CheckProtected.Checked = true;
73                 CheckProtected.Enabled = false;
74             }
75             else
76             {
77                 CheckProtected.Checked = _sts.Tabs[tabName].Protected;
78                 CheckProtected.Enabled = true;
79             }
80
81             CheckManageRead.Checked = _sts.Tabs[tabName].UnreadManage;
82             CheckNotifyNew.Checked = _sts.Tabs[tabName].Notify;
83
84             int idx = ComboSound.Items.IndexOf(_sts.Tabs[tabName].SoundFile);
85             if (idx == -1) idx = 0;
86             ComboSound.SelectedIndex = idx;
87
88             if (_directAdd) return;
89
90             ListTabs.Enabled = true;
91             GroupTab.Enabled = true;
92             ListFilters.Enabled = true;
93             if (ListFilters.SelectedIndex != -1)
94             {
95                 ShowDetail();
96             }
97             EditFilterGroup.Enabled = false;
98             switch (TabInformations.GetInstance().Tabs[tabName].TabType)
99             {
100                 case MyCommon.TabUsageType.Home:
101                 case MyCommon.TabUsageType.DirectMessage:
102                 case MyCommon.TabUsageType.Favorites:
103                 case MyCommon.TabUsageType.PublicSearch:
104                 case MyCommon.TabUsageType.Lists:
105                 case MyCommon.TabUsageType.Related:
106                 case MyCommon.TabUsageType.UserTimeline:
107                     ButtonNew.Enabled = false;
108                     ButtonEdit.Enabled = false;
109                     ButtonDelete.Enabled = false;
110                     ButtonRuleUp.Enabled = false;
111                     ButtonRuleDown.Enabled = false;
112                     ButtonRuleCopy.Enabled = false;
113                     ButtonRuleMove.Enabled = false;
114                     break;
115                 default:
116                     ButtonNew.Enabled = true;
117                     if (ListFilters.SelectedIndex > -1)
118                     {
119                         ButtonEdit.Enabled = true;
120                         ButtonDelete.Enabled = true;
121                         ButtonRuleUp.Enabled = true;
122                         ButtonRuleDown.Enabled = true;
123                         ButtonRuleCopy.Enabled = true;
124                         ButtonRuleMove.Enabled = true;
125                     }
126                     else
127                     {
128                         ButtonEdit.Enabled = false;
129                         ButtonDelete.Enabled = false;
130                         ButtonRuleUp.Enabled = false;
131                         ButtonRuleDown.Enabled = false;
132                         ButtonRuleCopy.Enabled = false;
133                         ButtonRuleMove.Enabled = false;
134                     }
135                     break;
136             }
137             switch (TabInformations.GetInstance().Tabs[tabName].TabType)
138             {
139                 case MyCommon.TabUsageType.Home:
140                     LabelTabType.Text = Properties.Resources.TabUsageTypeName_Home;
141                     break;
142                 case MyCommon.TabUsageType.Mentions:
143                     LabelTabType.Text = Properties.Resources.TabUsageTypeName_Mentions;
144                     break;
145                 case MyCommon.TabUsageType.DirectMessage:
146                     LabelTabType.Text = Properties.Resources.TabUsageTypeName_DirectMessage;
147                     break;
148                 case MyCommon.TabUsageType.Favorites:
149                     LabelTabType.Text = Properties.Resources.TabUsageTypeName_Favorites;
150                     break;
151                 case MyCommon.TabUsageType.UserDefined:
152                     LabelTabType.Text = Properties.Resources.TabUsageTypeName_UserDefined;
153                     break;
154                 case MyCommon.TabUsageType.PublicSearch:
155                     LabelTabType.Text = Properties.Resources.TabUsageTypeName_PublicSearch;
156                     break;
157                 case MyCommon.TabUsageType.Lists:
158                     LabelTabType.Text = Properties.Resources.TabUsageTypeName_Lists;
159                     break;
160                 case MyCommon.TabUsageType.Related:
161                     LabelTabType.Text = Properties.Resources.TabUsageTypeName_Related;
162                     break;
163                 case MyCommon.TabUsageType.UserTimeline:
164                     LabelTabType.Text = Properties.Resources.TabUsageTypeName_UserTimeline;
165                     break;
166                 default:
167                     LabelTabType.Text = "UNKNOWN";
168                     break;
169             }
170             ButtonRenameTab.Enabled = true;
171             if (TabInformations.GetInstance().IsDefaultTab(tabName) || TabInformations.GetInstance().Tabs[tabName].Protected)
172             {
173                 ButtonDeleteTab.Enabled = false;
174             }
175             else
176             {
177                 ButtonDeleteTab.Enabled = true;
178             }
179             ButtonClose.Enabled = true;
180         }
181
182         public void SetCurrent(string TabName)
183         {
184             _cur = TabName;
185         }
186
187         public void AddNewFilter(string id, string msg)
188         {
189             //元フォームから直接呼ばれる
190             ButtonNew.Enabled = false;
191             ButtonEdit.Enabled = false;
192             ButtonRuleUp.Enabled = false;
193             ButtonRuleDown.Enabled = false;
194             ButtonRuleCopy.Enabled = false;
195             ButtonRuleMove.Enabled = false;
196             ButtonDelete.Enabled = false;
197             ButtonClose.Enabled = false;
198             EditFilterGroup.Enabled = true;
199             ListTabs.Enabled = false;
200             GroupTab.Enabled = false;
201             ListFilters.Enabled = false;
202
203             RadioAND.Checked = true;
204             RadioPLUS.Checked = false;
205             UID.Text = id;
206             UID.SelectAll();
207             MSG1.Text = msg;
208             MSG1.SelectAll();
209             MSG2.Text = id + msg;
210             MSG2.SelectAll();
211             TextSource.Text = "";
212             UID.Enabled = true;
213             MSG1.Enabled = true;
214             MSG2.Enabled = false;
215             CheckRegex.Checked = false;
216             CheckURL.Checked = false;
217             CheckCaseSensitive.Checked = false;
218             CheckRetweet.Checked = false;
219             CheckLambda.Checked = false;
220
221             RadioExAnd.Checked = true;
222             RadioExPLUS.Checked = false;
223             ExUID.Text = "";
224             ExUID.SelectAll();
225             ExMSG1.Text = "";
226             ExMSG1.SelectAll();
227             ExMSG2.Text = "";
228             ExMSG2.SelectAll();
229             TextExSource.Text = "";
230             ExUID.Enabled = true;
231             ExMSG1.Enabled = true;
232             ExMSG2.Enabled = false;
233             CheckExRegex.Checked = false;
234             CheckExURL.Checked = false;
235             CheckExCaseSensitive.Checked = false;
236             CheckExRetweet.Checked = false;
237             CheckExLambDa.Checked = false;
238
239             OptCopy.Checked = true;
240             CheckMark.Checked = true;
241             UID.Focus();
242             _mode = EDITMODE.AddNew;
243             _directAdd = true;
244         }
245
246         private void ButtonNew_Click(object sender, EventArgs e)
247         {
248             ButtonNew.Enabled = false;
249             ButtonEdit.Enabled = false;
250             ButtonClose.Enabled = false;
251             ButtonRuleUp.Enabled = false;
252             ButtonRuleDown.Enabled = false;
253             ButtonRuleCopy.Enabled = false;
254             ButtonRuleMove.Enabled = false;
255             ButtonDelete.Enabled = false;
256             ButtonClose.Enabled = false;
257             EditFilterGroup.Enabled = true;
258             ListTabs.Enabled = false;
259             GroupTab.Enabled = false;
260             ListFilters.Enabled = false;
261
262             RadioAND.Checked = true;
263             RadioPLUS.Checked = false;
264             UID.Text = "";
265             MSG1.Text = "";
266             MSG2.Text = "";
267             TextSource.Text = "";
268             UID.Enabled = true;
269             MSG1.Enabled = true;
270             MSG2.Enabled = false;
271             CheckRegex.Checked = false;
272             CheckURL.Checked = false;
273             CheckCaseSensitive.Checked = false;
274             CheckRetweet.Checked = false;
275             CheckLambda.Checked = false;
276
277             RadioExAnd.Checked = true;
278             RadioExPLUS.Checked = false;
279             ExUID.Text = "";
280             ExMSG1.Text = "";
281             ExMSG2.Text = "";
282             TextExSource.Text = "";
283             ExUID.Enabled = true;
284             ExMSG1.Enabled = true;
285             ExMSG2.Enabled = false;
286             CheckExRegex.Checked = false;
287             CheckExURL.Checked = false;
288             CheckExCaseSensitive.Checked = false;
289             CheckExRetweet.Checked = false;
290             CheckExLambDa.Checked = false;
291
292             OptCopy.Checked = true;
293             CheckMark.Checked = true;
294             UID.Focus();
295             _mode = EDITMODE.AddNew;
296         }
297
298         private void ButtonEdit_Click(object sender, EventArgs e)
299         {
300             if (ListFilters.SelectedIndex == -1) return;
301
302             ShowDetail();
303
304             int idx = ListFilters.SelectedIndex;
305             ListFilters.SelectedIndex = -1;
306             ListFilters.SelectedIndex = idx;
307             ListFilters.Enabled = false;
308
309             ButtonNew.Enabled = false;
310             ButtonEdit.Enabled = false;
311             ButtonDelete.Enabled = false;
312             ButtonClose.Enabled = false;
313             ButtonRuleUp.Enabled = false;
314             ButtonRuleDown.Enabled = false;
315             ButtonRuleCopy.Enabled = false;
316             ButtonRuleMove.Enabled = false;
317             EditFilterGroup.Enabled = true;
318             ListTabs.Enabled = false;
319             GroupTab.Enabled = false;
320
321             _mode = EDITMODE.Edit;
322         }
323
324         private void ButtonDelete_Click(object sender, EventArgs e)
325         {
326             if (ListFilters.SelectedIndex == -1) return;
327             string tmp = "";
328             DialogResult rslt;
329
330             if (ListFilters.SelectedIndices.Count == 1)
331             {
332                 tmp = string.Format(Properties.Resources.ButtonDelete_ClickText1, "\r\n", ListFilters.SelectedItem.ToString());
333                 rslt = MessageBox.Show(tmp, Properties.Resources.ButtonDelete_ClickText2,
334                             MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
335             }
336             else
337             {
338                 tmp = string.Format(Properties.Resources.ButtonDelete_ClickText3, ListFilters.SelectedIndices.Count.ToString());
339                 rslt = MessageBox.Show(tmp, Properties.Resources.ButtonDelete_ClickText2,
340                             MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
341             }
342             if (rslt == DialogResult.Cancel) return;
343
344             for (int idx = ListFilters.Items.Count - 1; idx >= 0; idx--)
345             {
346                 if (ListFilters.GetSelected(idx))
347                 {
348                     _sts.Tabs[ListTabs.SelectedItem.ToString()].RemoveFilter((PostFilterRule)ListFilters.Items[idx]);
349                     ListFilters.Items.RemoveAt(idx);
350                 }
351             }
352         }
353
354         private void ButtonCancel_Click(object sender, EventArgs e)
355         {
356             ListTabs.Enabled = true;
357             GroupTab.Enabled = true;
358             ListFilters.Enabled = true;
359             ListFilters.Focus();
360             if (ListFilters.SelectedIndex != -1)
361             {
362                 ShowDetail();
363             }
364             EditFilterGroup.Enabled = false;
365             ButtonNew.Enabled = true;
366             if (ListFilters.SelectedIndex > -1)
367             {
368                 ButtonEdit.Enabled = true;
369                 ButtonDelete.Enabled = true;
370                 ButtonRuleUp.Enabled = true;
371                 ButtonRuleDown.Enabled = true;
372                 ButtonRuleCopy.Enabled = true;
373                 ButtonRuleMove.Enabled = true;
374             }
375             else
376             {
377                 ButtonEdit.Enabled = false;
378                 ButtonDelete.Enabled = false;
379                 ButtonRuleUp.Enabled = false;
380                 ButtonRuleDown.Enabled = false;
381                 ButtonRuleCopy.Enabled = false;
382                 ButtonRuleMove.Enabled = false;
383             }
384             ButtonClose.Enabled = true;
385             if (_directAdd)
386             {
387                 this.Close();
388             }
389         }
390
391         private void ShowDetail()
392         {
393             if (_directAdd) return;
394
395             if (ListFilters.SelectedIndex > -1)
396             {
397                 PostFilterRule fc = (PostFilterRule)ListFilters.SelectedItem;
398                 if (fc.UseNameField)
399                 {
400                     RadioAND.Checked = true;
401                     RadioPLUS.Checked = false;
402                     UID.Enabled = true;
403                     MSG1.Enabled = true;
404                     MSG2.Enabled = false;
405                     UID.Text = fc.FilterName;
406                     UID.SelectAll();
407                     MSG1.Text = string.Join(" ", fc.FilterBody);
408                     MSG1.SelectAll();
409                     MSG2.Text = "";
410                 }
411                 else
412                 {
413                     RadioPLUS.Checked = true;
414                     RadioAND.Checked = false;
415                     UID.Enabled = false;
416                     MSG1.Enabled = false;
417                     MSG2.Enabled = true;
418                     UID.Text = "";
419                     MSG1.Text = "";
420                     MSG2.Text = string.Join(" ", fc.FilterBody);
421                     MSG2.SelectAll();
422                 }
423                 TextSource.Text = fc.FilterSource;
424                 CheckRegex.Checked = fc.UseRegex;
425                 CheckURL.Checked = fc.FilterByUrl;
426                 CheckCaseSensitive.Checked = fc.CaseSensitive;
427                 CheckRetweet.Checked = fc.FilterRt;
428                 CheckLambda.Checked = fc.UseLambda;
429
430                 if (fc.ExUseNameField)
431                 {
432                     RadioExAnd.Checked = true;
433                     RadioExPLUS.Checked = false;
434                     ExUID.Enabled = true;
435                     ExMSG1.Enabled = true;
436                     ExMSG2.Enabled = false;
437                     ExUID.Text = fc.ExFilterName;
438                     ExUID.SelectAll();
439                     ExMSG1.Text = string.Join(" ", fc.ExFilterBody);
440                     ExMSG1.SelectAll();
441                     ExMSG2.Text = "";
442                 }
443                 else
444                 {
445                     RadioExPLUS.Checked = true;
446                     RadioExAnd.Checked = false;
447                     ExUID.Enabled = false;
448                     ExMSG1.Enabled = false;
449                     ExMSG2.Enabled = true;
450                     ExUID.Text = "";
451                     ExMSG1.Text = "";
452                     ExMSG2.Text = string.Join(" ", fc.ExFilterBody);
453                     ExMSG2.SelectAll();
454                 }
455                 TextExSource.Text = fc.ExFilterSource;
456                 CheckExRegex.Checked = fc.ExUseRegex;
457                 CheckExURL.Checked = fc.ExFilterByUrl;
458                 CheckExCaseSensitive.Checked = fc.ExCaseSensitive;
459                 CheckExRetweet.Checked = fc.ExFilterRt;
460                 CheckExLambDa.Checked = fc.ExUseLambda;
461
462                 if (fc.MoveMatches)
463                 {
464                     OptMove.Checked = true;
465                 }
466                 else
467                 {
468                     OptCopy.Checked = true;
469                 }
470                 CheckMark.Checked = fc.MarkMatches;
471
472                 ButtonEdit.Enabled = true;
473                 ButtonDelete.Enabled = true;
474                 ButtonRuleUp.Enabled = true;
475                 ButtonRuleDown.Enabled = true;
476                 ButtonRuleCopy.Enabled = true;
477                 ButtonRuleMove.Enabled = true;
478             }
479             else
480             {
481                 RadioAND.Checked = true;
482                 RadioPLUS.Checked = false;
483                 UID.Enabled = true;
484                 MSG1.Enabled = true;
485                 MSG2.Enabled = false;
486                 UID.Text = "";
487                 MSG1.Text = "";
488                 MSG2.Text = "";
489                 TextSource.Text = "";
490                 CheckRegex.Checked = false;
491                 CheckURL.Checked = false;
492                 CheckCaseSensitive.Checked = false;
493                 CheckRetweet.Checked = false;
494                 CheckLambda.Checked = false;
495
496                 RadioExAnd.Checked = true;
497                 RadioExPLUS.Checked = false;
498                 ExUID.Enabled = true;
499                 ExMSG1.Enabled = true;
500                 ExMSG2.Enabled = false;
501                 ExUID.Text = "";
502                 ExMSG1.Text = "";
503                 ExMSG2.Text = "";
504                 TextExSource.Text = "";
505                 CheckExRegex.Checked = false;
506                 CheckExURL.Checked = false;
507                 CheckExCaseSensitive.Checked = false;
508                 CheckExRetweet.Checked = false;
509                 CheckExLambDa.Checked = false;
510
511                 OptCopy.Checked = true;
512                 CheckMark.Checked = true;
513
514                 ButtonEdit.Enabled = false;
515                 ButtonDelete.Enabled = false;
516                 ButtonRuleUp.Enabled = false;
517                 ButtonRuleDown.Enabled = false;
518                 ButtonRuleCopy.Enabled = false;
519                 ButtonRuleMove.Enabled = false;
520             }
521         }
522
523         private void RadioAND_CheckedChanged(object sender, EventArgs e)
524         {
525             bool flg = RadioAND.Checked;
526             UID.Enabled = flg;
527             MSG1.Enabled = flg;
528             MSG2.Enabled = !flg;
529         }
530
531         private void ButtonOK_Click(object sender, EventArgs e)
532         {
533             bool isBlankMatch = false;
534             bool isBlankExclude = false;
535
536             //入力チェック
537             if (!CheckMatchRule(out isBlankMatch) || !CheckExcludeRule(out isBlankExclude))
538             {
539                 return;
540             }
541             if (isBlankMatch && isBlankExclude)
542             {
543                 MessageBox.Show(Properties.Resources.ButtonOK_ClickText1, Properties.Resources.ButtonOK_ClickText2, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
544                 return;
545             }
546
547             int i = ListFilters.SelectedIndex;
548             PostFilterRule ft;
549
550             ft = new PostFilterRule();
551
552             ft.MoveMatches = OptMove.Checked;
553             ft.MarkMatches = CheckMark.Checked;
554
555             string bdy = "";
556             if (RadioAND.Checked)
557             {
558                 ft.FilterName = UID.Text;
559                 TweenMain owner = (TweenMain)this.Owner;
560                 int cnt = owner.AtIdSupl.ItemCount;
561                 owner.AtIdSupl.AddItem("@" + ft.FilterName);
562                 if (cnt != owner.AtIdSupl.ItemCount)
563                 {
564                     owner.ModifySettingAtId = true;
565                 }
566                 ft.UseNameField = true;
567                 bdy = MSG1.Text;
568             }
569             else
570             {
571                 ft.FilterName = "";
572                 ft.UseNameField = false;
573                 bdy = MSG2.Text;
574             }
575             ft.FilterSource = TextSource.Text;
576
577             if (CheckRegex.Checked || CheckLambda.Checked)
578             {
579                 ft.FilterBody = new[] { bdy };
580             }
581             else
582             {
583                 ft.FilterBody = bdy.Split(' ', ' ')
584                     .Where(x => !string.IsNullOrEmpty(x))
585                     .ToArray();
586             }
587
588             ft.UseRegex = CheckRegex.Checked;
589             ft.FilterByUrl = CheckURL.Checked;
590             ft.CaseSensitive = CheckCaseSensitive.Checked;
591             ft.FilterRt = CheckRetweet.Checked;
592             ft.UseLambda = CheckLambda.Checked;
593
594             bdy = "";
595             if (RadioExAnd.Checked)
596             {
597                 ft.ExFilterName = ExUID.Text;
598                 ft.ExUseNameField = true;
599                 bdy = ExMSG1.Text;
600             }
601             else
602             {
603                 ft.ExFilterName = "";
604                 ft.ExUseNameField = false;
605                 bdy = ExMSG2.Text;
606             }
607             ft.ExFilterSource = TextExSource.Text;
608
609             if (CheckExRegex.Checked || CheckExLambDa.Checked)
610             {
611                 ft.ExFilterBody = new[] { bdy };
612             }
613             else
614             {
615                 ft.ExFilterBody = bdy.Split(' ', ' ')
616                     .Where(x => !string.IsNullOrEmpty(x))
617                     .ToArray();
618             }
619
620             ft.ExUseRegex = CheckExRegex.Checked;
621             ft.ExFilterByUrl = CheckExURL.Checked;
622             ft.ExCaseSensitive = CheckExCaseSensitive.Checked;
623             ft.ExFilterRt = CheckExRetweet.Checked;
624             ft.ExUseLambda = CheckExLambDa.Checked;
625
626             if (_mode == EDITMODE.AddNew)
627             {
628                 if (!_sts.Tabs[ListTabs.SelectedItem.ToString()].AddFilter(ft))
629                     MessageBox.Show(Properties.Resources.ButtonOK_ClickText4, Properties.Resources.ButtonOK_ClickText2, MessageBoxButtons.OK, MessageBoxIcon.Error);
630             }
631             else
632             {
633                 _sts.Tabs[ListTabs.SelectedItem.ToString()].EditFilter((PostFilterRule)ListFilters.SelectedItem, ft);
634             }
635
636             SetFilters(ListTabs.SelectedItem.ToString());
637             ListFilters.SelectedIndex = -1;
638             if (_mode == EDITMODE.AddNew)
639             {
640                 ListFilters.SelectedIndex = ListFilters.Items.Count - 1;
641             }
642             else
643             {
644                 ListFilters.SelectedIndex = i;
645             }
646             _mode = EDITMODE.None;
647
648             if (_directAdd)
649             {
650                 this.Close();
651             }
652         }
653
654         private bool IsValidLambdaExp(string text)
655         {
656             return false;
657             // TODO DynamicQuery相当のGPLv3互換なライブラリで置換する
658         }
659
660         private bool IsValidRegexp(string text)
661         {
662             try
663             {
664                 new Regex(text);
665             }
666             catch (Exception ex)
667             {
668                 MessageBox.Show(Properties.Resources.ButtonOK_ClickText3 + ex.Message, Properties.Resources.ButtonOK_ClickText2, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
669                 return false;
670             }
671             return true;
672         }
673
674         private bool CheckMatchRule(out bool isBlank)
675         {
676             isBlank = false;
677             if (RadioAND.Checked)
678             {
679                 if (string.IsNullOrEmpty(UID.Text) && string.IsNullOrEmpty(MSG1.Text) && string.IsNullOrEmpty(TextSource.Text) && CheckRetweet.Checked == false)
680                 {
681                     isBlank = true;
682                     return true;
683                 }
684                 if (CheckLambda.Checked)
685                 {
686                     if (!IsValidLambdaExp(UID.Text))
687                     {
688                         return false;
689                     }
690                     if (!IsValidLambdaExp(MSG1.Text))
691                     {
692                         return false;
693                     }
694                 }
695                 else if (CheckRegex.Checked)
696                 {
697                     if (!IsValidRegexp(UID.Text))
698                     {
699                         return false;
700                     }
701                     if (!IsValidRegexp(MSG1.Text))
702                     {
703                         return false;
704                     }
705                 }
706             }
707             else
708             {
709                 if (string.IsNullOrEmpty(MSG2.Text) && string.IsNullOrEmpty(TextSource.Text) && CheckRetweet.Checked == false)
710                 {
711                     isBlank = true;
712                     return true;
713                 }
714                 if (CheckLambda.Checked && !IsValidLambdaExp(MSG2.Text))
715                 {
716                     return false;
717                 }
718                 else if (CheckRegex.Checked && !IsValidRegexp(MSG2.Text))
719                 {
720                     return false;
721                 }
722             }
723
724             if (CheckRegex.Checked && !IsValidRegexp(TextSource.Text))
725             {
726                 return false;
727             }
728             return true;
729         }
730
731         private bool CheckExcludeRule(out bool isBlank)
732         {
733             isBlank = false;
734             if (RadioExAnd.Checked)
735             {
736                 if (string.IsNullOrEmpty(ExUID.Text) && string.IsNullOrEmpty(ExMSG1.Text) && string.IsNullOrEmpty(TextExSource.Text) && CheckExRetweet.Checked == false)
737                 {
738                     isBlank = true;
739                     return true;
740                 }
741                 if (CheckExLambDa.Checked)
742                 {
743                     if (!IsValidLambdaExp(ExUID.Text))
744                     {
745                         return false;
746                     }
747                     if (!IsValidLambdaExp(ExMSG1.Text))
748                     {
749                         return false;
750                     }
751                 }
752                 else if (CheckExRegex.Checked)
753                 {
754                     if (!IsValidRegexp(ExUID.Text))
755                     {
756                         return false;
757                     }
758                     if (!IsValidRegexp(ExMSG1.Text))
759                     {
760                         return false;
761                     }
762                 }
763             }
764             else
765             {
766                 if (string.IsNullOrEmpty(ExMSG2.Text) && string.IsNullOrEmpty(TextExSource.Text) && CheckExRetweet.Checked == false)
767                 {
768                     isBlank = true;
769                     return true;
770                 }
771                 if (CheckExLambDa.Checked && !IsValidLambdaExp(ExMSG2.Text))
772                 {
773                     return false;
774                 }
775                 else if (CheckExRegex.Checked && !IsValidRegexp(ExMSG2.Text))
776                 {
777                     return false;
778                 }
779             }
780
781             if (CheckExRegex.Checked && !IsValidRegexp(TextExSource.Text))
782             {
783                 return false;
784             }
785
786             return true;
787         }
788
789         private void ListFilters_SelectedIndexChanged(object sender, EventArgs e)
790         {
791             ShowDetail();
792         }
793
794         private void ButtonClose_Click(object sender, EventArgs e)
795         {
796             this.Close();
797         }
798
799         private void FilterDialog_FormClosed(object sender, FormClosedEventArgs e)
800         {
801             _directAdd = false;
802         }
803
804         private void FilterDialog_KeyDown(object sender, KeyEventArgs e)
805         {
806             if (e.KeyCode == Keys.Escape)
807             {
808                 if (EditFilterGroup.Enabled)
809                     ButtonCancel_Click(null, null);
810                 else
811                     ButtonClose_Click(null, null);
812             }
813         }
814
815         private void ListFilters_DoubleClick(object sender, EventArgs e)
816         {
817             if (ListFilters.SelectedItem == null)
818             {
819                 return;
820             }
821
822             if (ListFilters.IndexFromPoint(ListFilters.PointToClient(Control.MousePosition)) == ListBox.NoMatches)
823             {
824                 return;
825             }
826
827             if (ListFilters.Items[ListFilters.IndexFromPoint(ListFilters.PointToClient(Control.MousePosition))] == null)
828             {
829                 return;
830             }
831             ButtonEdit_Click(sender, e);
832         }
833
834         private void FilterDialog_Shown(object sender, EventArgs e)
835         {
836             _sts = TabInformations.GetInstance();
837             ListTabs.Items.Clear();
838             foreach (string key in _sts.Tabs.Keys)
839             {
840                 ListTabs.Items.Add(key);
841             }
842
843             ComboSound.Items.Clear();
844             ComboSound.Items.Add("");
845             DirectoryInfo oDir = new DirectoryInfo(Application.StartupPath + Path.DirectorySeparatorChar);
846             if (Directory.Exists(Path.Combine(Application.StartupPath, "Sounds")))
847             {
848                 oDir = oDir.GetDirectories("Sounds")[0];
849             }
850             foreach (FileInfo oFile in oDir.GetFiles("*.wav"))
851             {
852                 ComboSound.Items.Add(oFile.Name);
853             }
854
855             idlist.Clear();
856             foreach (string tmp in ((TweenMain)this.Owner).AtIdSupl.GetItemList())
857             {
858                 idlist.Add(tmp.Remove(0, 1));  // @文字削除
859             }
860             UID.AutoCompleteCustomSource.Clear();
861             UID.AutoCompleteCustomSource.AddRange(idlist.ToArray());
862
863             ExUID.AutoCompleteCustomSource.Clear();
864             ExUID.AutoCompleteCustomSource.AddRange(idlist.ToArray());
865
866             //選択タブ変更
867             if (ListTabs.Items.Count > 0)
868             {
869                 if (_cur.Length > 0)
870                 {
871                     for (int i = 0; i < ListTabs.Items.Count; i++)
872                     {
873                         if (_cur == ListTabs.Items[i].ToString())
874                         {
875                             ListTabs.SelectedIndex = i;
876                             //tabdialog.TabList.Items.Remove(_cur);
877                             break;
878                         }
879                     }
880                 }
881             }
882         }
883
884         private void ListTabs_SelectedIndexChanged(object sender, EventArgs e)
885         {
886             if (ListTabs.SelectedIndex > -1)
887                 SetFilters(ListTabs.SelectedItem.ToString());
888             else
889                 ListFilters.Items.Clear();
890         }
891
892         private void ButtonAddTab_Click(object sender, EventArgs e)
893         {
894             string tabName = null;
895             MyCommon.TabUsageType tabType;
896             using (InputTabName inputName = new InputTabName())
897             {
898                 inputName.TabName = _sts.GetUniqueTabName();
899                 inputName.IsShowUsage = true;
900                 inputName.ShowDialog();
901                 if (inputName.DialogResult == DialogResult.Cancel) return;
902                 tabName = inputName.TabName;
903                 tabType = inputName.Usage;
904             }
905             if (!string.IsNullOrEmpty(tabName))
906             {
907                 //List対応
908                 ListElement list = null;
909                 if (tabType == MyCommon.TabUsageType.Lists)
910                 {
911                     string rslt = ((TweenMain)this.Owner).TwitterInstance.GetListsApi();
912                     if (!string.IsNullOrEmpty(rslt))
913                     {
914                         MessageBox.Show("Failed to get lists. (" + rslt + ")");
915                     }
916                     using (ListAvailable listAvail = new ListAvailable())
917                     {
918                         if (listAvail.ShowDialog(this) == DialogResult.Cancel) return;
919                         if (listAvail.SelectedList == null) return;
920                         list = listAvail.SelectedList;
921                     }
922                 }
923                 if (!_sts.AddTab(tabName, tabType, list) || !((TweenMain)this.Owner).AddNewTab(tabName, false, tabType, list))
924                 {
925                     string tmp = string.Format(Properties.Resources.AddTabMenuItem_ClickText1, tabName);
926                     MessageBox.Show(tmp, Properties.Resources.AddTabMenuItem_ClickText2, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
927                     return;
928                 }
929                 else
930                 {
931                     //成功
932                     ListTabs.Items.Add(tabName);
933                 }
934             }
935         }
936
937         private void ButtonDeleteTab_Click(object sender, EventArgs e)
938         {
939             if (ListTabs.SelectedIndex > -1 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
940             {
941                 string tb = ListTabs.SelectedItem.ToString();
942                 int idx = ListTabs.SelectedIndex;
943                 if (((TweenMain)this.Owner).RemoveSpecifiedTab(tb, true))
944                 {
945                     ListTabs.Items.RemoveAt(idx);
946                     idx -= 1;
947                     if (idx < 0) idx = 0;
948                     ListTabs.SelectedIndex = idx;
949                 }
950             }
951         }
952
953         private void ButtonRenameTab_Click(object sender, EventArgs e)
954         {
955             if (ListTabs.SelectedIndex > -1 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
956             {
957                 string tb = ListTabs.SelectedItem.ToString();
958                 int idx = ListTabs.SelectedIndex;
959                 if (((TweenMain)this.Owner).TabRename(ref tb))
960                 {
961                     ListTabs.Items.RemoveAt(idx);
962                     ListTabs.Items.Insert(idx, tb);
963                     ListTabs.SelectedIndex = idx;
964                 }
965             }
966         }
967
968         private void CheckManageRead_CheckedChanged(object sender, EventArgs e)
969         {
970             if (ListTabs.SelectedIndex > -1 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
971             {
972                 ((TweenMain)this.Owner).ChangeTabUnreadManage(
973                     ListTabs.SelectedItem.ToString(),
974                     CheckManageRead.Checked);
975             }
976         }
977
978         private void ButtonUp_Click(object sender, EventArgs e)
979         {
980             if (ListTabs.SelectedIndex > 0 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
981             {
982                 string selName = ListTabs.SelectedItem.ToString();
983                 string tgtName = ListTabs.Items[ListTabs.SelectedIndex - 1].ToString();
984                 ((TweenMain)this.Owner).ReOrderTab(
985                     selName,
986                     tgtName,
987                     true);
988                 int idx = ListTabs.SelectedIndex;
989                 ListTabs.Items.RemoveAt(idx - 1);
990                 ListTabs.Items.Insert(idx, tgtName);
991             }
992         }
993
994         private void ButtonDown_Click(object sender, EventArgs e)
995         {
996             if (ListTabs.SelectedIndex > -1 && ListTabs.SelectedIndex < ListTabs.Items.Count - 1 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
997             {
998                 string selName = ListTabs.SelectedItem.ToString();
999                 string tgtName = ListTabs.Items[ListTabs.SelectedIndex + 1].ToString();
1000                 ((TweenMain)this.Owner).ReOrderTab(
1001                     selName,
1002                     tgtName,
1003                     false);
1004                 int idx = ListTabs.SelectedIndex;
1005                 ListTabs.Items.RemoveAt(idx + 1);
1006                 ListTabs.Items.Insert(idx, tgtName);
1007             }
1008         }
1009
1010         private void CheckLocked_CheckedChanged(object sender, EventArgs e)
1011         {
1012             if (ListTabs.SelectedIndex > -1 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
1013             {
1014                 _sts.Tabs[ListTabs.SelectedItem.ToString()].Protected = CheckProtected.Checked;
1015                 ButtonDeleteTab.Enabled = !CheckProtected.Checked;
1016             }
1017         }
1018
1019         private void CheckNotifyNew_CheckedChanged(object sender, EventArgs e)
1020         {
1021             if (ListTabs.SelectedIndex > -1 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
1022             {
1023                 _sts.Tabs[ListTabs.SelectedItem.ToString()].Notify = CheckNotifyNew.Checked;
1024             }
1025         }
1026
1027         private void ComboSound_SelectedIndexChanged(object sender, EventArgs e)
1028         {
1029             if (ListTabs.SelectedIndex > -1 && string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
1030             {
1031                 string filename = "";
1032                 if (ComboSound.SelectedIndex > -1) filename = ComboSound.SelectedItem.ToString();
1033                 _sts.Tabs[ListTabs.SelectedItem.ToString()].SoundFile = filename;
1034             }
1035         }
1036
1037         private void RadioExAnd_CheckedChanged(object sender, EventArgs e)
1038         {
1039             bool flg = RadioExAnd.Checked;
1040             ExUID.Enabled = flg;
1041             ExMSG1.Enabled = flg;
1042             ExMSG2.Enabled = !flg;
1043         }
1044
1045         private void OptMove_CheckedChanged(object sender, EventArgs e)
1046         {
1047             CheckMark.Enabled = !OptMove.Checked;
1048         }
1049
1050         private void ButtonRuleUp_Click(object sender, EventArgs e)
1051         {
1052             if (ListTabs.SelectedIndex > -1 && ListFilters.SelectedItem != null && ListFilters.SelectedIndex > 0)
1053             {
1054                 string tabname = ListTabs.SelectedItem.ToString();
1055                 PostFilterRule target = _sts.Tabs[tabname].Filters[ListFilters.SelectedIndex - 1];
1056                 int idx = ListFilters.SelectedIndex;
1057                 ListFilters.Items.RemoveAt(idx - 1);
1058                 ListFilters.Items.Insert(idx, target);
1059                 _sts.Tabs[tabname].Filters.RemoveAt(idx - 1);
1060                 _sts.Tabs[tabname].Filters.Insert(idx, target);
1061             }
1062         }
1063
1064         private void ButtonRuleDown_Click(object sender, EventArgs e)
1065         {
1066             if (ListTabs.SelectedIndex > -1 && ListFilters.SelectedItem != null && ListFilters.SelectedIndex < ListFilters.Items.Count - 1)
1067             {
1068                 string tabname = ListTabs.SelectedItem.ToString();
1069                 PostFilterRule target = _sts.Tabs[tabname].Filters[ListFilters.SelectedIndex + 1];
1070                 int idx = ListFilters.SelectedIndex;
1071                 ListFilters.Items.RemoveAt(idx + 1);
1072                 ListFilters.Items.Insert(idx, target);
1073                 _sts.Tabs[tabname].Filters.RemoveAt(idx + 1);
1074                 _sts.Tabs[tabname].Filters.Insert(idx, target);
1075             }
1076         }
1077
1078         private void ButtonRuleCopy_Click(object sender, EventArgs e)
1079         {
1080             if (ListTabs.SelectedIndex > -1 && ListFilters.SelectedItem != null)
1081             {
1082                 TabClass[] selectedTabs;
1083                 using (TabsDialog dialog = new TabsDialog(_sts))
1084                 {
1085                     dialog.MultiSelect = true;
1086                     dialog.Text = Properties.Resources.ButtonRuleCopy_ClickText1;
1087
1088                     if (dialog.ShowDialog(this) == DialogResult.Cancel) return;
1089
1090                     selectedTabs = dialog.SelectedTabs;
1091                 }
1092
1093                 string tabname = ListTabs.SelectedItem.ToString();
1094                 List<PostFilterRule> filters = new List<PostFilterRule>();
1095
1096                 foreach (int idx in ListFilters.SelectedIndices)
1097                 {
1098                     filters.Add(_sts.Tabs[tabname].Filters[idx].Clone());
1099                 }
1100                 foreach (var tb in selectedTabs)
1101                 {
1102                     if (tb.TabName == tabname) continue;
1103
1104                     foreach (PostFilterRule flt in filters)
1105                     {
1106                         if (!tb.Filters.Contains(flt))
1107                             tb.AddFilter(flt.Clone());
1108                     }
1109                 }
1110                 SetFilters(tabname);
1111             }
1112         }
1113
1114         private void ButtonRuleMove_Click(object sender, EventArgs e)
1115         {
1116             if (ListTabs.SelectedIndex > -1 && ListFilters.SelectedItem != null)
1117             {
1118                 TabClass[] selectedTabs;
1119                 using (var dialog = new TabsDialog(_sts))
1120                 {
1121                     dialog.MultiSelect = true;
1122                     dialog.Text = Properties.Resources.ButtonRuleMove_ClickText1;
1123
1124                     if (dialog.ShowDialog(this) == DialogResult.Cancel) return;
1125
1126                     selectedTabs = dialog.SelectedTabs;
1127                 }
1128                 string tabname = ListTabs.SelectedItem.ToString();
1129                 List<PostFilterRule> filters = new List<PostFilterRule>();
1130
1131                 foreach (int idx in ListFilters.SelectedIndices)
1132                 {
1133                     filters.Add(_sts.Tabs[tabname].Filters[idx].Clone());
1134                 }
1135                 if (selectedTabs.Length == 1 && selectedTabs[0].TabName == tabname) return;
1136                 foreach (var tb in selectedTabs)
1137                 {
1138                     if (tb.TabName == tabname) continue;
1139
1140                     foreach (PostFilterRule flt in filters)
1141                     {
1142                         if (!tb.Filters.Contains(flt))
1143                             tb.AddFilter(flt.Clone());
1144                     }
1145                 }
1146                 for (int idx = ListFilters.Items.Count - 1; idx >= 0; idx--)
1147                 {
1148                     if (ListFilters.GetSelected(idx))
1149                     {
1150                         _sts.Tabs[ListTabs.SelectedItem.ToString()].RemoveFilter((PostFilterRule)ListFilters.Items[idx]);
1151                         ListFilters.Items.RemoveAt(idx);
1152                     }
1153                 }
1154                 SetFilters(tabname);
1155             }
1156         }
1157
1158         private void FilterTextBox_KeyDown(object sender, KeyEventArgs e)
1159         {
1160             if (e.KeyCode == Keys.Space && e.Modifiers == (Keys.Shift | Keys.Control))
1161             {
1162                 TweenMain main = (TweenMain)this.Owner;
1163                 TextBox tbox = (TextBox)sender;
1164                 if (tbox.SelectionStart > 0)
1165                 {
1166                     int endidx = tbox.SelectionStart - 1;
1167                     string startstr = "";
1168                     for (int i = tbox.SelectionStart - 1; i >= 0; i--)
1169                     {
1170                         char c = tbox.Text[i];
1171                         if (Char.IsLetterOrDigit(c) || c == '_')
1172                         {
1173                             continue;
1174                         }
1175                         if (c == '@')
1176                         {
1177                             startstr = tbox.Text.Substring(i + 1, endidx - i);
1178                             main.ShowSuplDialog(tbox, main.AtIdSupl, startstr.Length + 1, startstr);
1179                         }
1180                         else if (c == '#')
1181                         {
1182                             startstr = tbox.Text.Substring(i + 1, endidx - i);
1183                             main.ShowSuplDialog(tbox, main.HashSupl, startstr.Length + 1, startstr);
1184                         }
1185                         else
1186                         {
1187                             break;
1188                         }
1189                     }
1190                     e.Handled = true;
1191                 }
1192             }
1193         }
1194
1195         private void FilterTextBox_KeyPress(object sender, KeyPressEventArgs e)
1196         {
1197             TweenMain main = (TweenMain)this.Owner;
1198             TextBox tbox = (TextBox)sender;
1199             if (e.KeyChar == '@')
1200             {
1201                 //if (!SettingDialog.UseAtIdSupplement) return;
1202                 //@マーク
1203                 main.ShowSuplDialog(tbox, main.AtIdSupl);
1204                 e.Handled = true;
1205             }
1206             else if (e.KeyChar == '#')
1207             {
1208                 //if (!SettingDialog.UseHashSupplement) return;
1209                 main.ShowSuplDialog(tbox, main.HashSupl);
1210                 e.Handled = true;
1211             }
1212         }
1213     }
1214 }