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.
10 // This file is part of OpenTween.
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)
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
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.
28 using System.Collections.Generic;
29 using System.ComponentModel;
34 using System.Windows.Forms;
35 using System.Linq.Expressions;
36 using System.Text.RegularExpressions;
38 using System.Collections.Specialized;
42 public partial class FilterDialog : OTBaseForm
44 private EDITMODE _mode;
45 private bool _directAdd;
46 private bool _moveRules = false;
47 private TabInformations _sts;
49 private List<string> idlist = new List<string>();
60 InitializeComponent();
63 private void SetFilters(string tabName)
65 if (ListTabs.Items.Count == 0) return;
67 var tab = _sts.Tabs[tabName];
69 ListFilters.Items.Clear();
70 ListFilters.Items.AddRange(tab.GetFilters());
72 if (ListFilters.Items.Count > 0)
73 ListFilters.SelectedIndex = 0;
77 if (tab.IsDefaultTabType)
79 CheckProtected.Checked = true;
80 CheckProtected.Enabled = false;
84 CheckProtected.Checked = tab.Protected;
85 CheckProtected.Enabled = true;
88 CheckManageRead.Checked = tab.UnreadManage;
89 CheckNotifyNew.Checked = tab.Notify;
91 int idx = ComboSound.Items.IndexOf(tab.SoundFile);
92 if (idx == -1) idx = 0;
93 ComboSound.SelectedIndex = idx;
95 if (_directAdd) return;
97 if (tab.TabType == MyCommon.TabUsageType.Mute)
99 this.CheckManageRead.Enabled = false;
100 this.CheckNotifyNew.Enabled = false;
101 this.ComboSound.Enabled = false;
103 this.GroupBox1.Visible = false;
104 this.labelMuteTab.Visible = true;
108 this.CheckManageRead.Enabled = true;
109 this.CheckNotifyNew.Enabled = true;
110 this.ComboSound.Enabled = true;
112 this.GroupBox1.Visible = true;
113 this.labelMuteTab.Visible = false;
116 ListTabs.Enabled = true;
117 GroupTab.Enabled = true;
118 ListFilters.Enabled = true;
119 EditFilterGroup.Enabled = false;
122 case MyCommon.TabUsageType.Home:
123 case MyCommon.TabUsageType.DirectMessage:
124 case MyCommon.TabUsageType.Favorites:
125 case MyCommon.TabUsageType.PublicSearch:
126 case MyCommon.TabUsageType.Lists:
127 case MyCommon.TabUsageType.Related:
128 case MyCommon.TabUsageType.UserTimeline:
129 ButtonNew.Enabled = false;
130 ButtonEdit.Enabled = false;
131 ButtonDelete.Enabled = false;
132 ButtonRuleUp.Enabled = false;
133 ButtonRuleDown.Enabled = false;
134 ButtonRuleCopy.Enabled = false;
135 ButtonRuleMove.Enabled = false;
138 ButtonNew.Enabled = true;
139 if (ListFilters.SelectedIndex > -1)
141 ButtonEdit.Enabled = true;
142 ButtonDelete.Enabled = true;
143 ButtonRuleUp.Enabled = true;
144 ButtonRuleDown.Enabled = true;
145 ButtonRuleCopy.Enabled = true;
146 ButtonRuleMove.Enabled = true;
150 ButtonEdit.Enabled = false;
151 ButtonDelete.Enabled = false;
152 ButtonRuleUp.Enabled = false;
153 ButtonRuleDown.Enabled = false;
154 ButtonRuleCopy.Enabled = false;
155 ButtonRuleMove.Enabled = false;
161 case MyCommon.TabUsageType.Home:
162 LabelTabType.Text = Properties.Resources.TabUsageTypeName_Home;
164 case MyCommon.TabUsageType.Mentions:
165 LabelTabType.Text = Properties.Resources.TabUsageTypeName_Mentions;
167 case MyCommon.TabUsageType.DirectMessage:
168 LabelTabType.Text = Properties.Resources.TabUsageTypeName_DirectMessage;
170 case MyCommon.TabUsageType.Favorites:
171 LabelTabType.Text = Properties.Resources.TabUsageTypeName_Favorites;
173 case MyCommon.TabUsageType.UserDefined:
174 LabelTabType.Text = Properties.Resources.TabUsageTypeName_UserDefined;
176 case MyCommon.TabUsageType.PublicSearch:
177 LabelTabType.Text = Properties.Resources.TabUsageTypeName_PublicSearch;
179 case MyCommon.TabUsageType.Lists:
180 LabelTabType.Text = Properties.Resources.TabUsageTypeName_Lists;
182 case MyCommon.TabUsageType.Related:
183 LabelTabType.Text = Properties.Resources.TabUsageTypeName_Related;
185 case MyCommon.TabUsageType.UserTimeline:
186 LabelTabType.Text = Properties.Resources.TabUsageTypeName_UserTimeline;
188 case MyCommon.TabUsageType.Mute:
189 LabelTabType.Text = "Mute";
192 LabelTabType.Text = "UNKNOWN";
195 ButtonRenameTab.Enabled = true;
196 if (tab.IsDefaultTabType || tab.Protected)
198 ButtonDeleteTab.Enabled = false;
202 ButtonDeleteTab.Enabled = true;
204 ButtonClose.Enabled = true;
207 public void SetCurrent(string TabName)
212 public void AddNewFilter(string id, string msg)
215 ButtonNew.Enabled = false;
216 ButtonEdit.Enabled = false;
217 ButtonRuleUp.Enabled = false;
218 ButtonRuleDown.Enabled = false;
219 ButtonRuleCopy.Enabled = false;
220 ButtonRuleMove.Enabled = false;
221 ButtonDelete.Enabled = false;
222 ButtonClose.Enabled = false;
223 EditFilterGroup.Enabled = true;
224 ListTabs.Enabled = false;
225 GroupTab.Enabled = false;
226 ListFilters.Enabled = false;
228 RadioAND.Checked = true;
229 RadioPLUS.Checked = false;
234 MSG2.Text = id + msg;
236 TextSource.Text = "";
239 MSG2.Enabled = false;
240 CheckRegex.Checked = false;
241 CheckURL.Checked = false;
242 CheckCaseSensitive.Checked = false;
243 CheckRetweet.Checked = false;
244 CheckLambda.Checked = false;
246 RadioExAnd.Checked = true;
247 RadioExPLUS.Checked = false;
254 TextExSource.Text = "";
255 ExUID.Enabled = true;
256 ExMSG1.Enabled = true;
257 ExMSG2.Enabled = false;
258 CheckExRegex.Checked = false;
259 CheckExURL.Checked = false;
260 CheckExCaseSensitive.Checked = false;
261 CheckExRetweet.Checked = false;
262 CheckExLambDa.Checked = false;
264 OptCopy.Checked = true;
265 CheckMark.Checked = true;
267 _mode = EDITMODE.AddNew;
271 private void ButtonNew_Click(object sender, EventArgs e)
273 ButtonNew.Enabled = false;
274 ButtonEdit.Enabled = false;
275 ButtonClose.Enabled = false;
276 ButtonRuleUp.Enabled = false;
277 ButtonRuleDown.Enabled = false;
278 ButtonRuleCopy.Enabled = false;
279 ButtonRuleMove.Enabled = false;
280 ButtonDelete.Enabled = false;
281 ButtonClose.Enabled = false;
282 EditFilterGroup.Enabled = true;
283 ListTabs.Enabled = false;
284 GroupTab.Enabled = false;
285 ListFilters.Enabled = false;
287 RadioAND.Checked = true;
288 RadioPLUS.Checked = false;
292 TextSource.Text = "";
295 MSG2.Enabled = false;
296 CheckRegex.Checked = false;
297 CheckURL.Checked = false;
298 CheckCaseSensitive.Checked = false;
299 CheckRetweet.Checked = false;
300 CheckLambda.Checked = false;
302 RadioExAnd.Checked = true;
303 RadioExPLUS.Checked = false;
307 TextExSource.Text = "";
308 ExUID.Enabled = true;
309 ExMSG1.Enabled = true;
310 ExMSG2.Enabled = false;
311 CheckExRegex.Checked = false;
312 CheckExURL.Checked = false;
313 CheckExCaseSensitive.Checked = false;
314 CheckExRetweet.Checked = false;
315 CheckExLambDa.Checked = false;
317 OptCopy.Checked = true;
318 CheckMark.Checked = true;
320 _mode = EDITMODE.AddNew;
323 private void ButtonEdit_Click(object sender, EventArgs e)
325 if (ListFilters.SelectedIndex == -1) return;
329 int idx = ListFilters.SelectedIndex;
330 ListFilters.SelectedIndex = -1;
331 ListFilters.SelectedIndex = idx;
332 ListFilters.Enabled = false;
334 ButtonNew.Enabled = false;
335 ButtonEdit.Enabled = false;
336 ButtonDelete.Enabled = false;
337 ButtonClose.Enabled = false;
338 ButtonRuleUp.Enabled = false;
339 ButtonRuleDown.Enabled = false;
340 ButtonRuleCopy.Enabled = false;
341 ButtonRuleMove.Enabled = false;
342 EditFilterGroup.Enabled = true;
343 ListTabs.Enabled = false;
344 GroupTab.Enabled = false;
346 _mode = EDITMODE.Edit;
349 private void ButtonDelete_Click(object sender, EventArgs e)
351 var selectedCount = ListFilters.SelectedIndices.Count;
352 if (selectedCount == 0) return;
356 if (selectedCount == 1)
358 tmp = string.Format(Properties.Resources.ButtonDelete_ClickText1, Environment.NewLine, ListFilters.SelectedItem.ToString());
362 tmp = string.Format(Properties.Resources.ButtonDelete_ClickText3, selectedCount.ToString());
365 var rslt = MessageBox.Show(tmp, Properties.Resources.ButtonDelete_ClickText2, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
366 if (rslt == DialogResult.Cancel) return;
368 var indices = ListFilters.SelectedIndices.Cast<int>().Reverse().ToArray(); // 後ろの要素から削除
369 var tab = _sts.Tabs[ListTabs.SelectedItem.ToString()];
371 using (ControlTransaction.Update(ListFilters))
373 foreach (var idx in indices)
375 tab.RemoveFilter((PostFilterRule)ListFilters.Items[idx]);
376 ListFilters.Items.RemoveAt(idx);
381 private void ButtonCancel_Click(object sender, EventArgs e)
383 ListTabs.Enabled = true;
384 GroupTab.Enabled = true;
385 ListFilters.Enabled = true;
387 if (ListFilters.SelectedIndex != -1)
391 EditFilterGroup.Enabled = false;
392 ButtonNew.Enabled = true;
393 if (ListFilters.SelectedIndex > -1)
395 ButtonEdit.Enabled = true;
396 ButtonDelete.Enabled = true;
397 ButtonRuleUp.Enabled = true;
398 ButtonRuleDown.Enabled = true;
399 ButtonRuleCopy.Enabled = true;
400 ButtonRuleMove.Enabled = true;
404 ButtonEdit.Enabled = false;
405 ButtonDelete.Enabled = false;
406 ButtonRuleUp.Enabled = false;
407 ButtonRuleDown.Enabled = false;
408 ButtonRuleCopy.Enabled = false;
409 ButtonRuleMove.Enabled = false;
411 ButtonClose.Enabled = true;
418 private void ShowDetail()
420 if (_directAdd) return;
422 if (ListFilters.SelectedIndex > -1)
424 PostFilterRule fc = (PostFilterRule)ListFilters.SelectedItem;
427 RadioAND.Checked = true;
428 RadioPLUS.Checked = false;
431 MSG2.Enabled = false;
432 UID.Text = fc.FilterName;
434 MSG1.Text = string.Join(" ", fc.FilterBody);
440 RadioPLUS.Checked = true;
441 RadioAND.Checked = false;
443 MSG1.Enabled = false;
447 MSG2.Text = string.Join(" ", fc.FilterBody);
450 TextSource.Text = fc.FilterSource;
451 CheckRegex.Checked = fc.UseRegex;
452 CheckURL.Checked = fc.FilterByUrl;
453 CheckCaseSensitive.Checked = fc.CaseSensitive;
454 CheckRetweet.Checked = fc.FilterRt;
455 CheckLambda.Checked = fc.UseLambda;
457 if (fc.ExUseNameField)
459 RadioExAnd.Checked = true;
460 RadioExPLUS.Checked = false;
461 ExUID.Enabled = true;
462 ExMSG1.Enabled = true;
463 ExMSG2.Enabled = false;
464 ExUID.Text = fc.ExFilterName;
466 ExMSG1.Text = string.Join(" ", fc.ExFilterBody);
472 RadioExPLUS.Checked = true;
473 RadioExAnd.Checked = false;
474 ExUID.Enabled = false;
475 ExMSG1.Enabled = false;
476 ExMSG2.Enabled = true;
479 ExMSG2.Text = string.Join(" ", fc.ExFilterBody);
482 TextExSource.Text = fc.ExFilterSource;
483 CheckExRegex.Checked = fc.ExUseRegex;
484 CheckExURL.Checked = fc.ExFilterByUrl;
485 CheckExCaseSensitive.Checked = fc.ExCaseSensitive;
486 CheckExRetweet.Checked = fc.ExFilterRt;
487 CheckExLambDa.Checked = fc.ExUseLambda;
491 OptMove.Checked = true;
495 OptCopy.Checked = true;
497 CheckMark.Checked = fc.MarkMatches;
499 ButtonEdit.Enabled = true;
500 ButtonDelete.Enabled = true;
501 ButtonRuleUp.Enabled = true;
502 ButtonRuleDown.Enabled = true;
503 ButtonRuleCopy.Enabled = true;
504 ButtonRuleMove.Enabled = true;
508 RadioAND.Checked = true;
509 RadioPLUS.Checked = false;
512 MSG2.Enabled = false;
516 TextSource.Text = "";
517 CheckRegex.Checked = false;
518 CheckURL.Checked = false;
519 CheckCaseSensitive.Checked = false;
520 CheckRetweet.Checked = false;
521 CheckLambda.Checked = false;
523 RadioExAnd.Checked = true;
524 RadioExPLUS.Checked = false;
525 ExUID.Enabled = true;
526 ExMSG1.Enabled = true;
527 ExMSG2.Enabled = false;
531 TextExSource.Text = "";
532 CheckExRegex.Checked = false;
533 CheckExURL.Checked = false;
534 CheckExCaseSensitive.Checked = false;
535 CheckExRetweet.Checked = false;
536 CheckExLambDa.Checked = false;
538 OptCopy.Checked = true;
539 CheckMark.Checked = true;
541 ButtonEdit.Enabled = false;
542 ButtonDelete.Enabled = false;
543 ButtonRuleUp.Enabled = false;
544 ButtonRuleDown.Enabled = false;
545 ButtonRuleCopy.Enabled = false;
546 ButtonRuleMove.Enabled = false;
550 private void RadioAND_CheckedChanged(object sender, EventArgs e)
552 bool flg = RadioAND.Checked;
558 private void ButtonOK_Click(object sender, EventArgs e)
560 bool isBlankMatch = false;
561 bool isBlankExclude = false;
564 if (!CheckMatchRule(out isBlankMatch) || !CheckExcludeRule(out isBlankExclude))
568 if (isBlankMatch && isBlankExclude)
570 MessageBox.Show(Properties.Resources.ButtonOK_ClickText1, Properties.Resources.ButtonOK_ClickText2, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
574 var tab = this._sts.Tabs[(string)this.ListTabs.SelectedItem];
575 int i = ListFilters.SelectedIndex;
578 if (_mode == EDITMODE.AddNew)
579 ft = new PostFilterRule();
581 ft = (PostFilterRule)this.ListFilters.SelectedItem;
583 if (tab.TabType != MyCommon.TabUsageType.Mute)
585 ft.MoveMatches = OptMove.Checked;
586 ft.MarkMatches = CheckMark.Checked;
590 ft.MoveMatches = true;
591 ft.MarkMatches = false;
595 if (RadioAND.Checked)
597 ft.FilterName = UID.Text;
598 TweenMain owner = (TweenMain)this.Owner;
599 int cnt = owner.AtIdSupl.ItemCount;
600 owner.AtIdSupl.AddItem("@" + ft.FilterName);
601 if (cnt != owner.AtIdSupl.ItemCount)
603 owner.ModifySettingAtId = true;
605 ft.UseNameField = true;
611 ft.UseNameField = false;
614 ft.FilterSource = TextSource.Text;
616 if (CheckRegex.Checked || CheckLambda.Checked)
618 ft.FilterBody = new[] { bdy };
622 ft.FilterBody = bdy.Split(' ', ' ')
623 .Where(x => !string.IsNullOrEmpty(x))
627 ft.UseRegex = CheckRegex.Checked;
628 ft.FilterByUrl = CheckURL.Checked;
629 ft.CaseSensitive = CheckCaseSensitive.Checked;
630 ft.FilterRt = CheckRetweet.Checked;
631 ft.UseLambda = CheckLambda.Checked;
634 if (RadioExAnd.Checked)
636 ft.ExFilterName = ExUID.Text;
637 ft.ExUseNameField = true;
642 ft.ExFilterName = "";
643 ft.ExUseNameField = false;
646 ft.ExFilterSource = TextExSource.Text;
648 if (CheckExRegex.Checked || CheckExLambDa.Checked)
650 ft.ExFilterBody = new[] { bdy };
654 ft.ExFilterBody = bdy.Split(' ', ' ')
655 .Where(x => !string.IsNullOrEmpty(x))
659 ft.ExUseRegex = CheckExRegex.Checked;
660 ft.ExFilterByUrl = CheckExURL.Checked;
661 ft.ExCaseSensitive = CheckExCaseSensitive.Checked;
662 ft.ExFilterRt = CheckExRetweet.Checked;
663 ft.ExUseLambda = CheckExLambDa.Checked;
665 if (_mode == EDITMODE.AddNew)
667 if (!tab.AddFilter(ft))
668 MessageBox.Show(Properties.Resources.ButtonOK_ClickText4, Properties.Resources.ButtonOK_ClickText2, MessageBoxButtons.OK, MessageBoxIcon.Error);
671 SetFilters(tab.TabName);
672 ListFilters.SelectedIndex = -1;
673 if (_mode == EDITMODE.AddNew)
675 ListFilters.SelectedIndex = ListFilters.Items.Count - 1;
679 ListFilters.SelectedIndex = i;
681 _mode = EDITMODE.None;
689 private bool IsValidLambdaExp(string text)
692 // TODO DynamicQuery相当のGPLv3互換なライブラリで置換する
695 private bool IsValidRegexp(string text)
703 MessageBox.Show(Properties.Resources.ButtonOK_ClickText3 + ex.Message, Properties.Resources.ButtonOK_ClickText2, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
709 private bool CheckMatchRule(out bool isBlank)
712 if (RadioAND.Checked)
714 if (string.IsNullOrEmpty(UID.Text) && string.IsNullOrEmpty(MSG1.Text) && string.IsNullOrEmpty(TextSource.Text) && CheckRetweet.Checked == false)
719 if (CheckLambda.Checked)
721 if (!IsValidLambdaExp(UID.Text))
725 if (!IsValidLambdaExp(MSG1.Text))
730 else if (CheckRegex.Checked)
732 if (!IsValidRegexp(UID.Text))
736 if (!IsValidRegexp(MSG1.Text))
744 if (string.IsNullOrEmpty(MSG2.Text) && string.IsNullOrEmpty(TextSource.Text) && CheckRetweet.Checked == false)
749 if (CheckLambda.Checked && !IsValidLambdaExp(MSG2.Text))
753 else if (CheckRegex.Checked && !IsValidRegexp(MSG2.Text))
759 if (CheckRegex.Checked && !IsValidRegexp(TextSource.Text))
766 private bool CheckExcludeRule(out bool isBlank)
769 if (RadioExAnd.Checked)
771 if (string.IsNullOrEmpty(ExUID.Text) && string.IsNullOrEmpty(ExMSG1.Text) && string.IsNullOrEmpty(TextExSource.Text) && CheckExRetweet.Checked == false)
776 if (CheckExLambDa.Checked)
778 if (!IsValidLambdaExp(ExUID.Text))
782 if (!IsValidLambdaExp(ExMSG1.Text))
787 else if (CheckExRegex.Checked)
789 if (!IsValidRegexp(ExUID.Text))
793 if (!IsValidRegexp(ExMSG1.Text))
801 if (string.IsNullOrEmpty(ExMSG2.Text) && string.IsNullOrEmpty(TextExSource.Text) && CheckExRetweet.Checked == false)
806 if (CheckExLambDa.Checked && !IsValidLambdaExp(ExMSG2.Text))
810 else if (CheckExRegex.Checked && !IsValidRegexp(ExMSG2.Text))
816 if (CheckExRegex.Checked && !IsValidRegexp(TextExSource.Text))
824 private void ListFilters_SelectedIndexChanged(object sender, EventArgs e)
830 private void ButtonClose_Click(object sender, EventArgs e)
835 private void FilterDialog_FormClosed(object sender, FormClosedEventArgs e)
840 private void FilterDialog_KeyDown(object sender, KeyEventArgs e)
842 if (e.KeyCode == Keys.Escape)
844 if (EditFilterGroup.Enabled)
845 ButtonCancel_Click(null, null);
847 ButtonClose_Click(null, null);
851 private void ListFilters_DoubleClick(object sender, EventArgs e)
853 var idx = ListFilters.SelectedIndex;
854 if (idx == -1) return;
856 var midx = ListFilters.IndexFromPoint(ListFilters.PointToClient(Control.MousePosition));
857 if (midx == ListBox.NoMatches || idx != midx) return;
859 ButtonEdit_Click(sender, e);
862 private void FilterDialog_Shown(object sender, EventArgs e)
864 _sts = TabInformations.GetInstance();
865 ListTabs.Items.Clear();
866 foreach (string key in _sts.Tabs.Keys)
868 ListTabs.Items.Add(key);
871 ComboSound.Items.Clear();
872 ComboSound.Items.Add("");
873 DirectoryInfo oDir = new DirectoryInfo(Application.StartupPath + Path.DirectorySeparatorChar);
874 if (Directory.Exists(Path.Combine(Application.StartupPath, "Sounds")))
876 oDir = oDir.GetDirectories("Sounds")[0];
878 foreach (FileInfo oFile in oDir.GetFiles("*.wav"))
880 ComboSound.Items.Add(oFile.Name);
884 foreach (string tmp in ((TweenMain)this.Owner).AtIdSupl.GetItemList())
886 idlist.Add(tmp.Remove(0, 1)); // @文字削除
888 UID.AutoCompleteCustomSource.Clear();
889 UID.AutoCompleteCustomSource.AddRange(idlist.ToArray());
891 ExUID.AutoCompleteCustomSource.Clear();
892 ExUID.AutoCompleteCustomSource.AddRange(idlist.ToArray());
895 if (ListTabs.Items.Count > 0)
899 for (int i = 0; i < ListTabs.Items.Count; i++)
901 if (_cur == ListTabs.Items[i].ToString())
903 ListTabs.SelectedIndex = i;
904 //tabdialog.TabList.Items.Remove(_cur);
912 private void ListTabs_SelectedIndexChanged(object sender, EventArgs e)
914 if (ListTabs.SelectedIndex > -1)
915 SetFilters(ListTabs.SelectedItem.ToString());
917 ListFilters.Items.Clear();
920 private void ButtonAddTab_Click(object sender, EventArgs e)
922 string tabName = null;
923 MyCommon.TabUsageType tabType;
924 using (InputTabName inputName = new InputTabName())
926 inputName.TabName = _sts.GetUniqueTabName();
927 inputName.IsShowUsage = true;
928 inputName.ShowDialog();
929 if (inputName.DialogResult == DialogResult.Cancel) return;
930 tabName = inputName.TabName;
931 tabType = inputName.Usage;
933 if (!string.IsNullOrEmpty(tabName))
936 ListElement list = null;
937 if (tabType == MyCommon.TabUsageType.Lists)
939 string rslt = ((TweenMain)this.Owner).TwitterInstance.GetListsApi();
940 if (!string.IsNullOrEmpty(rslt))
942 MessageBox.Show("Failed to get lists. (" + rslt + ")");
944 using (ListAvailable listAvail = new ListAvailable())
946 if (listAvail.ShowDialog(this) == DialogResult.Cancel) return;
947 if (listAvail.SelectedList == null) return;
948 list = listAvail.SelectedList;
951 if (!_sts.AddTab(tabName, tabType, list) || !((TweenMain)this.Owner).AddNewTab(tabName, false, tabType, list))
953 string tmp = string.Format(Properties.Resources.AddTabMenuItem_ClickText1, tabName);
954 MessageBox.Show(tmp, Properties.Resources.AddTabMenuItem_ClickText2, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
960 ListTabs.Items.Add(tabName);
965 private void ButtonDeleteTab_Click(object sender, EventArgs e)
967 if (ListTabs.SelectedIndex > -1 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
969 string tb = ListTabs.SelectedItem.ToString();
970 int idx = ListTabs.SelectedIndex;
971 if (((TweenMain)this.Owner).RemoveSpecifiedTab(tb, true))
973 ListTabs.Items.RemoveAt(idx);
975 if (idx < 0) idx = 0;
976 ListTabs.SelectedIndex = idx;
981 private void ButtonRenameTab_Click(object sender, EventArgs e)
983 if (ListTabs.SelectedIndex > -1 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
985 string tb = ListTabs.SelectedItem.ToString();
986 int idx = ListTabs.SelectedIndex;
987 if (((TweenMain)this.Owner).TabRename(ref tb))
989 ListTabs.Items.RemoveAt(idx);
990 ListTabs.Items.Insert(idx, tb);
991 ListTabs.SelectedIndex = idx;
996 private void CheckManageRead_CheckedChanged(object sender, EventArgs e)
998 if (ListTabs.SelectedIndex > -1 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
1000 ((TweenMain)this.Owner).ChangeTabUnreadManage(
1001 ListTabs.SelectedItem.ToString(),
1002 CheckManageRead.Checked);
1006 private void ButtonUp_Click(object sender, EventArgs e)
1008 if (this.ListTabs.SelectedIndex == -1 || this.ListTabs.SelectedIndex == 0)
1011 var selectedTabName = (string)this.ListTabs.SelectedItem;
1012 var selectedTab = this._sts.Tabs[selectedTabName];
1014 var targetTabName = (string)this.ListTabs.Items[this.ListTabs.SelectedIndex - 1];
1015 var targetTab = this._sts.Tabs[targetTabName];
1018 if (selectedTab.TabType == MyCommon.TabUsageType.Mute || targetTab.TabType == MyCommon.TabUsageType.Mute)
1021 var tweenMain = (TweenMain)this.Owner;
1022 tweenMain.ReOrderTab(selectedTabName, targetTabName, true);
1024 // ListTab のアイテム並び替え
1025 // 選択が解除されてしまうのを防ぐため SelectedIndex のアイテムは操作せず前後のアイテムを移動する
1026 var idx = this.ListTabs.SelectedIndex;
1027 this.ListTabs.Items.RemoveAt(idx - 1);
1028 this.ListTabs.Items.Insert(idx, targetTabName);
1031 private void ButtonDown_Click(object sender, EventArgs e)
1033 if (this.ListTabs.SelectedIndex == -1 || this.ListTabs.SelectedIndex == this.ListTabs.Items.Count - 1)
1036 var selectedTabName = (string)this.ListTabs.SelectedItem;
1037 var selectedTab = this._sts.Tabs[selectedTabName];
1039 var targetTabName = (string)this.ListTabs.Items[this.ListTabs.SelectedIndex + 1];
1040 var targetTab = this._sts.Tabs[targetTabName];
1043 if (selectedTab.TabType == MyCommon.TabUsageType.Mute || targetTab.TabType == MyCommon.TabUsageType.Mute)
1046 var tweenMain = (TweenMain)this.Owner;
1047 tweenMain.ReOrderTab(selectedTabName, targetTabName, false);
1049 // ListTab のアイテム並び替え
1050 // 選択が解除されてしまうのを防ぐため SelectedIndex のアイテムは操作せず前後のアイテムを移動する
1051 var idx = this.ListTabs.SelectedIndex;
1052 this.ListTabs.Items.RemoveAt(idx + 1);
1053 this.ListTabs.Items.Insert(idx, targetTabName);
1056 private void CheckLocked_CheckedChanged(object sender, EventArgs e)
1058 if (ListTabs.SelectedIndex > -1 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
1060 _sts.Tabs[ListTabs.SelectedItem.ToString()].Protected = CheckProtected.Checked;
1061 ButtonDeleteTab.Enabled = !CheckProtected.Checked;
1065 private void CheckNotifyNew_CheckedChanged(object sender, EventArgs e)
1067 if (ListTabs.SelectedIndex > -1 && !string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
1069 _sts.Tabs[ListTabs.SelectedItem.ToString()].Notify = CheckNotifyNew.Checked;
1073 private void ComboSound_SelectedIndexChanged(object sender, EventArgs e)
1075 if (ListTabs.SelectedIndex > -1 && string.IsNullOrEmpty(ListTabs.SelectedItem.ToString()))
1077 string filename = "";
1078 if (ComboSound.SelectedIndex > -1) filename = ComboSound.SelectedItem.ToString();
1079 _sts.Tabs[ListTabs.SelectedItem.ToString()].SoundFile = filename;
1083 private void RadioExAnd_CheckedChanged(object sender, EventArgs e)
1085 bool flg = RadioExAnd.Checked;
1086 ExUID.Enabled = flg;
1087 ExMSG1.Enabled = flg;
1088 ExMSG2.Enabled = !flg;
1091 private void OptMove_CheckedChanged(object sender, EventArgs e)
1093 CheckMark.Enabled = !OptMove.Checked;
1096 private void ButtonRuleUp_Click(object sender, EventArgs e)
1098 MoveSelectedRules(up: true);
1101 private void ButtonRuleDown_Click(object sender, EventArgs e)
1103 MoveSelectedRules(up: false);
1106 private void MoveSelectedRules(bool up)
1108 var tabIdx = ListTabs.SelectedIndex;
1110 ListFilters.SelectedIndices.Count == 0) return;
1112 var indices = ListFilters.SelectedIndices.Cast<int>().ToArray();
1117 if (indices[0] <= 0) return;
1122 if (indices[indices.Length - 1] >= ListFilters.Items.Count - 1) return;
1124 Array.Reverse(indices); // 逆順にして、下にある要素から処理する
1127 var lastSelIdx = indices[0] + diff;
1128 var tab = _sts.Tabs[ListTabs.Items[tabIdx].ToString()];
1132 _moveRules = true; // SelectedIndexChanged を無視する
1134 using (ControlTransaction.Update(ListFilters))
1136 ListFilters.SelectedIndices.Clear();
1138 foreach (var idx in indices)
1140 var tidx = idx + diff;
1141 var target = (PostFilterRule)ListFilters.Items[tidx];
1143 // 移動先にある要素と位置を入れ替える
1144 ListFilters.Items.RemoveAt(tidx);
1145 ListFilters.Items.Insert(idx, target);
1147 // 移動方向の先頭要素以外なら選択する
1148 if (tidx != lastSelIdx)
1149 ListFilters.SelectedIndex = tidx;
1152 tab.FilterArray = ListFilters.Items.Cast<PostFilterRule>().ToArray();
1154 // 移動方向の先頭要素は最後に選択する
1156 ListFilters.SelectedIndex = lastSelIdx;
1165 private void ButtonRuleCopy_Click(object sender, EventArgs e)
1167 if (ListTabs.SelectedIndex > -1 && ListFilters.SelectedItem != null)
1169 TabClass[] selectedTabs;
1170 using (TabsDialog dialog = new TabsDialog(_sts))
1172 dialog.MultiSelect = true;
1173 dialog.Text = Properties.Resources.ButtonRuleCopy_ClickText1;
1175 if (dialog.ShowDialog(this) == DialogResult.Cancel) return;
1177 selectedTabs = dialog.SelectedTabs;
1180 string tabname = ListTabs.SelectedItem.ToString();
1181 List<PostFilterRule> filters = new List<PostFilterRule>();
1183 foreach (int idx in ListFilters.SelectedIndices)
1185 filters.Add(_sts.Tabs[tabname].FilterArray[idx].Clone());
1187 foreach (var tb in selectedTabs)
1189 if (tb.TabName == tabname) continue;
1191 foreach (PostFilterRule flt in filters)
1193 if (!tb.FilterArray.Contains(flt))
1194 tb.AddFilter(flt.Clone());
1197 SetFilters(tabname);
1201 private void ButtonRuleMove_Click(object sender, EventArgs e)
1203 if (ListTabs.SelectedIndex > -1 && ListFilters.SelectedItem != null)
1205 TabClass[] selectedTabs;
1206 using (var dialog = new TabsDialog(_sts))
1208 dialog.MultiSelect = true;
1209 dialog.Text = Properties.Resources.ButtonRuleMove_ClickText1;
1211 if (dialog.ShowDialog(this) == DialogResult.Cancel) return;
1213 selectedTabs = dialog.SelectedTabs;
1215 string tabname = ListTabs.SelectedItem.ToString();
1216 List<PostFilterRule> filters = new List<PostFilterRule>();
1218 foreach (int idx in ListFilters.SelectedIndices)
1220 filters.Add(_sts.Tabs[tabname].FilterArray[idx].Clone());
1222 if (selectedTabs.Length == 1 && selectedTabs[0].TabName == tabname) return;
1223 foreach (var tb in selectedTabs)
1225 if (tb.TabName == tabname) continue;
1227 foreach (PostFilterRule flt in filters)
1229 if (!tb.FilterArray.Contains(flt))
1230 tb.AddFilter(flt.Clone());
1233 for (int idx = ListFilters.Items.Count - 1; idx >= 0; idx--)
1235 if (ListFilters.GetSelected(idx))
1237 _sts.Tabs[ListTabs.SelectedItem.ToString()].RemoveFilter((PostFilterRule)ListFilters.Items[idx]);
1238 ListFilters.Items.RemoveAt(idx);
1241 SetFilters(tabname);
1245 private void FilterTextBox_KeyDown(object sender, KeyEventArgs e)
1247 if (e.KeyCode == Keys.Space && e.Modifiers == (Keys.Shift | Keys.Control))
1249 TweenMain main = (TweenMain)this.Owner;
1250 TextBox tbox = (TextBox)sender;
1251 if (tbox.SelectionStart > 0)
1253 int endidx = tbox.SelectionStart - 1;
1254 string startstr = "";
1255 for (int i = tbox.SelectionStart - 1; i >= 0; i--)
1257 char c = tbox.Text[i];
1258 if (Char.IsLetterOrDigit(c) || c == '_')
1264 startstr = tbox.Text.Substring(i + 1, endidx - i);
1265 main.ShowSuplDialog(tbox, main.AtIdSupl, startstr.Length + 1, startstr);
1269 startstr = tbox.Text.Substring(i + 1, endidx - i);
1270 main.ShowSuplDialog(tbox, main.HashSupl, startstr.Length + 1, startstr);
1282 private void FilterTextBox_KeyPress(object sender, KeyPressEventArgs e)
1284 TweenMain main = (TweenMain)this.Owner;
1285 TextBox tbox = (TextBox)sender;
1286 if (e.KeyChar == '@')
1288 //if (!SettingDialog.UseAtIdSupplement) return;
1290 main.ShowSuplDialog(tbox, main.AtIdSupl);
1293 else if (e.KeyChar == '#')
1295 //if (!SettingDialog.UseHashSupplement) return;
1296 main.ShowSuplDialog(tbox, main.HashSupl);
1301 private void ListFilters_DrawItem(object sender, DrawItemEventArgs e)
1305 var filter = (PostFilterRule)this.ListFilters.Items[e.Index];
1309 textBrush = SystemBrushes.ControlText;
1311 textBrush = SystemBrushes.GrayText;
1313 e.Graphics.DrawString(filter.ToString(), e.Font, textBrush, e.Bounds);
1315 e.DrawFocusRectangle();
1318 protected override void OnFontChanged(EventArgs e)
1320 base.OnFontChanged(e);
1321 this.ListFilters.ItemHeight = this.ListFilters.Font.Height;