OSDN Git Service

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