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.
31 using System.Collections.Generic;
32 using System.ComponentModel;
35 using System.Threading.Tasks;
37 namespace OpenTween.Models
39 public class FilterTabModel : TabModel
41 public override MyCommon.TabUsageType TabType
42 => MyCommon.TabUsageType.UserDefined;
44 public bool FilterModified { get; set; }
46 private readonly List<PostFilterRule> _filters = new List<PostFilterRule>();
47 private readonly object lockObjFilters = new object();
49 public FilterTabModel(string tabName) : base(tabName)
54 public MyCommon.HITRESULT AddFiltered(PostClass post, bool immediately = false)
56 if (this.IsInnerStorageTabType)
57 return MyCommon.HITRESULT.None;
59 var rslt = MyCommon.HITRESULT.None;
62 lock (this.lockObjFilters)
64 foreach (var ft in _filters)
68 switch (ft.ExecFilter(post)) //フィルタクラスでヒット判定
70 case MyCommon.HITRESULT.None:
72 case MyCommon.HITRESULT.Copy:
73 if (rslt != MyCommon.HITRESULT.CopyAndMark) rslt = MyCommon.HITRESULT.Copy;
75 case MyCommon.HITRESULT.CopyAndMark:
76 rslt = MyCommon.HITRESULT.CopyAndMark;
78 case MyCommon.HITRESULT.Move:
79 rslt = MyCommon.HITRESULT.Move;
81 case MyCommon.HITRESULT.Exclude:
82 rslt = MyCommon.HITRESULT.Exclude;
86 catch (NullReferenceException)
88 // ExecFilterでNullRef出る場合あり。暫定対応
89 MyCommon.TraceOut("ExecFilterでNullRef: " + ft);
90 rslt = MyCommon.HITRESULT.None;
97 if (this.TabType != MyCommon.TabUsageType.Mute &&
98 rslt != MyCommon.HITRESULT.None && rslt != MyCommon.HITRESULT.Exclude)
101 this.AddPostImmediately(post.StatusId, post.IsRead);
103 this.AddPostQueue(post);
106 return rslt; //マーク付けは呼び出し元で行うこと
110 public PostFilterRule[] GetFilters()
112 lock (this.lockObjFilters)
114 return _filters.ToArray();
118 public void RemoveFilter(PostFilterRule filter)
120 lock (this.lockObjFilters)
122 _filters.Remove(filter);
123 filter.PropertyChanged -= this.OnFilterModified;
124 this.FilterModified = true;
128 public bool AddFilter(PostFilterRule filter)
130 lock (this.lockObjFilters)
132 if (_filters.Contains(filter)) return false;
133 filter.PropertyChanged += this.OnFilterModified;
134 _filters.Add(filter);
135 this.FilterModified = true;
140 private void OnFilterModified(object sender, PropertyChangedEventArgs e)
141 => this.FilterModified = true;
143 public PostFilterRule[] FilterArray
147 lock (this.lockObjFilters)
149 return _filters.ToArray();
154 lock (this.lockObjFilters)
156 foreach (var oldFilter in this._filters)
158 oldFilter.PropertyChanged -= this.OnFilterModified;
161 this._filters.Clear();
162 this.FilterModified = true;
164 foreach (var newFilter in value)
166 _filters.Add(newFilter);
167 newFilter.PropertyChanged += this.OnFilterModified;
173 public override Task RefreshAsync(Twitter tw, bool backward, bool startup, IProgress<string> progress)
175 var homeTab = TabInformations.GetInstance().HomeTab;
177 return homeTab.RefreshAsync(tw, backward, startup, progress);