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 Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 // (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
9 // All rights reserved.
11 // This file is part of OpenTween.
13 // This program is free software; you can redistribute it and/or modify it
14 // under the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 3 of the License, or (at your option)
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
25 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 // Boston, MA 02110-1301, USA.
29 using System.Collections.Generic;
30 using System.ComponentModel;
33 using System.Threading.Tasks;
35 namespace OpenTween.Models
37 public class FilterTabModel : TabModel
39 public override MyCommon.TabUsageType TabType
40 => MyCommon.TabUsageType.UserDefined;
42 public bool FilterModified { get; set; }
44 private readonly List<PostFilterRule> _filters = new List<PostFilterRule>();
45 private readonly object lockObjFilters = new object();
47 public FilterTabModel(string tabName) : base(tabName)
52 public MyCommon.HITRESULT AddFiltered(PostClass post, bool immediately = false)
54 if (this.IsInnerStorageTabType)
55 return MyCommon.HITRESULT.None;
57 var rslt = MyCommon.HITRESULT.None;
60 lock (this.lockObjFilters)
62 foreach (var ft in _filters)
66 switch (ft.ExecFilter(post)) //フィルタクラスでヒット判定
68 case MyCommon.HITRESULT.None:
70 case MyCommon.HITRESULT.Copy:
71 if (rslt != MyCommon.HITRESULT.CopyAndMark) rslt = MyCommon.HITRESULT.Copy;
73 case MyCommon.HITRESULT.CopyAndMark:
74 rslt = MyCommon.HITRESULT.CopyAndMark;
76 case MyCommon.HITRESULT.Move:
77 rslt = MyCommon.HITRESULT.Move;
79 case MyCommon.HITRESULT.Exclude:
80 rslt = MyCommon.HITRESULT.Exclude;
84 catch (NullReferenceException)
86 // ExecFilterでNullRef出る場合あり。暫定対応
87 MyCommon.TraceOut("ExecFilterでNullRef: " + ft);
88 rslt = MyCommon.HITRESULT.None;
95 if (this.TabType != MyCommon.TabUsageType.Mute &&
96 rslt != MyCommon.HITRESULT.None && rslt != MyCommon.HITRESULT.Exclude)
99 this.AddPostImmediately(post.StatusId, post.IsRead);
101 this.AddPostQueue(post);
104 return rslt; //マーク付けは呼び出し元で行うこと
108 public PostFilterRule[] GetFilters()
110 lock (this.lockObjFilters)
112 return _filters.ToArray();
116 public void RemoveFilter(PostFilterRule filter)
118 lock (this.lockObjFilters)
120 _filters.Remove(filter);
121 filter.PropertyChanged -= this.OnFilterModified;
122 this.FilterModified = true;
126 public bool AddFilter(PostFilterRule filter)
128 lock (this.lockObjFilters)
130 if (_filters.Contains(filter)) return false;
131 filter.PropertyChanged += this.OnFilterModified;
132 _filters.Add(filter);
133 this.FilterModified = true;
138 private void OnFilterModified(object sender, PropertyChangedEventArgs e)
139 => this.FilterModified = true;
141 public PostFilterRule[] FilterArray
145 lock (this.lockObjFilters)
147 return _filters.ToArray();
152 lock (this.lockObjFilters)
154 foreach (var oldFilter in this._filters)
156 oldFilter.PropertyChanged -= this.OnFilterModified;
159 this._filters.Clear();
160 this.FilterModified = true;
162 foreach (var newFilter in value)
164 _filters.Add(newFilter);
165 newFilter.PropertyChanged += this.OnFilterModified;
171 public override Task RefreshAsync(Twitter tw, bool backward, bool startup, IProgress<string> progress)
173 var homeTab = TabInformations.GetInstance().GetTabByType<HomeTabModel>();
175 return homeTab.RefreshAsync(tw, backward, startup, progress);