OSDN Git Service

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